i got a program that stores words and counts the number of occurences of that
word and im trying to print them from an arraylist
it just displays Chapter18.CountingOccurences
[Chapter18.WordOccurence@26e431, Chapter18.WordOccurence@14f8dab, Chapter18.WordOccurence@1ddebc3, Chapter18.WordOccurence@a18aa2, Chapter18.WordOccurence@194ca6c, Chapter18.WordOccurence@17590db, Chapter18.WordOccurence@17943a4, Chapter18.WordOccurence@480457, Chapter18.WordOccurence@14fe5c, Chapter18.WordOccurence@47858e, Chapter18.WordOccurence@19134f4, Chapter18.WordOccurence@2bbd86, Chapter18.WordOccurence@1a7bf11, Chapter18.WordOccurence@1f12c4e]
but i want to do it so it displays the word and the count
import java.util.*;
public class CountingOccurences
{
public static void main(
String [] args)
{
String text = "Have a good day, Have a good class. " +
"Have a good visit. Have fun!";
Map hashMap = new HashMap();
List arrayList = new ArrayList();
StringTokenizer st = new StringTokenizer(text, " .!?");
while (st.hasMoreTokens())
{
String key = st.nextToken();
if(hashMap.get(key) != null)
{
int value = ((Integer)hashMap.get(key)).intValue();
value++;
hashMap.put(key, new Integer(value));
}
else
{
hashMap.put(key, new Integer(1));
}
arrayList.add(new WordOccurence(key, ((Integer)hashMap.get(key)).intValue()));
}
Collections.sort(arrayList);
System.out.println(arrayList);
}
}
public class WordOccurence implements Comparable
{
String word;
int count = 0;
public int compareTo(Object o)
{
if (this.count == ((WordOccurence)o).count)
{
return 0;
}
else if (this.count > ((WordOccurence)o).count)
{
return 1;
}
else
{
return -1;
}
}
public WordOccurence(String word, int count)
{
this.word = word;
this.count = count;
}
}
[ October 31, 2006: Message edited by: Brent Geeeee ]