• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need help displaying an ordered array list by count

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
We're being followed by intergalactic spies! Quick! Take this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic