Character Frequency Array
The problem I am working on asks me to have the user type text into a TextArea, and then count the frequency of all letters and display them, then display which letter was most frequent and how many times it was encountered.
I know I need an array for this, but how to implement it? I was thinking of something like this:
Code:
char charArr[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z'};
And then using a loop to check if the current character is a letter and if so, match it with one of the characters in the array and increment a counter. But will I need a seperate counter for each letter, or should I use a temp variable to store the current letter and if it matches increment a counter, and then overwrite it?
This one uses no array fiddling....
...but if the use of arrays was a requirement then this solution is wrong.
However, using arrays for solving this is not very sensible....
Code:
import java.util.*;
/**
* Single Character count class
*/
class CharCount implements Comparator {
public Character ch=null;
public Integer count=null;
public CharCount(){}
public CharCount(Character ch, Integer count) {
this.ch=ch;
this.count=count;
}
public String toString() {
return ch.toString()+"\t"+count.toString();
}
public int compare(Object o1, Object o2) {
CharCount c1=(CharCount)o1;
CharCount c2=(CharCount)o2;
return c2.count.intValue() - c1.count.intValue();
}
public boolean equals(Object obj) {
CharCount cc=(CharCount)obj;
return cc.ch.equals(ch) && cc.count.equals(count);
}
}
/**
* Count character frequency in a String and generate a
* ranking list of characters and frequency count.
*/
public class TokenCounter {
ArrayList rankList=null;
public TokenCounter(String s) {
Hashtable ht=getCountHashTable(s);
sortCharacterCount(ht);
}
public ArrayList getResult() {
return rankList;
}
/**
* Sort on character frequency
*/
private ArrayList sortCharacterCount(Hashtable ht) {
rankList=new ArrayList();
Enumeration enum = ht.keys();
while (enum.hasMoreElements()) {
Character ch = (Character)enum.nextElement();
Integer count=(Integer)ht.get(ch);
rankList.add(new CharCount(ch, count));
}
Collections.sort(rankList,new CharCount());
return rankList;
}
/**
* Count frequencies
*/
private Hashtable getCountHashTable(String s) {
Hashtable cHt=new Hashtable();
char [] cBuf=new char[s.length()];
s.getChars(0,cBuf.length,cBuf,0);
for (int i=0; i<cBuf.length; i++) {
if (Character.isLetter(cBuf[i])) { // count letters only
Character ch = new Character(cBuf[i]);
Integer cnt = (Integer) cHt.get(ch);
if (cnt == null) {
cHt.put(ch, new Integer(1));
}
else {
cHt.put(ch, new Integer(cnt.intValue() + 1));
}
}
}
return cHt;
}
public static void main(String[] args) {
String s="This is a test strrrringgg 11 22 33";
TokenCounter tc = new TokenCounter(s);
ArrayList list=tc.getResult();
System.out.println("Character frequency for:\n["+s+"]");
for (int i=0; i<list.size(); i++) {
System.out.println(list.get(i).toString());
}
}
}