This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

need help displaying an ordered array list by count  RSS feed

 
Brent Geeeee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

From a brief look at your code it seems that you are just printing out the arraylist, which is a list of objects. This means the output you are getting is correct.

To produce the output you want you would have to loop through the ArrayList pulling calling the attributes from each object pulled back.

Hope that helps,

John
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

you should override toString() in class word occurence:
eg


Yours,
Bu.
[ October 31, 2006: Message edited by: Burkhard Hassel ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!