File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes need help displaying an ordered array list by count Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "need help displaying an ordered array list by count" Watch "need help displaying an ordered array list by count" New topic
Author

need help displaying an ordered array list by count

Brent Geeeee
Greenhorn

Joined: Mar 15, 2006
Posts: 2
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

Joined: Jan 25, 2006
Posts: 116
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

Joined: Aug 25, 2006
Posts: 1274
Hi,

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


Yours,
Bu.
[ October 31, 2006: Message edited by: Burkhard Hassel ]

all events occur in real time
 
I agree. Here's the link: jrebel
 
subject: need help displaying an ordered array list by count
 
Similar Threads
Oracle help
Performing Math on Integer Class
HashMap
Question about Tree Node --Word Tree
Hashmap, retrieving value and re-storing with different key