Frequency Count of letters in text
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   corey_taylor
Posted On:   Monday, April 9, 2001 10:04 PM

I have a problem: I'm trying to make a program that will read multiple lines of text, count the occurrences of the letters of the alphabet, and display that summary information. If anyone could possibly help me, please post or email. I do have a bulk of the code, I just can't get it to work. Thank You in advance!

Re: Frequency Count of letters in text

Posted By:   Michael_Wax  
Posted On:   Tuesday, April 10, 2001 05:07 PM

Use a HashMap to maintain a count by character in the text.


// 'body' is a String containing the text you have read in
HashMap map = new HashMap(26);
char current;
for (int i=0; i current = temp.charAt(i);
//skip if not a letter
if (!Character.isLetter(current)) continue;
//'a' and 'A' are the same letter
String temp = String.valueOf(current)toUpperCase();
if (map.get(temp) != null) {
int count = ((Integer) map.getTemp()).intValue();
map.put(temp, new Integer(count++));
}
else map.put(temp, new Integer(1));
}
About | Sitemap | Contact