pls help me with word occurances program
i have a program which reads a file and displays the number of occurances of each word in the file and also sorts it occording to number of occurances
i want to get only top 10 words from the file pls help me with the code
the program is:
Code Java:
import java.io.*;
import java.util.*;
public class HashMapEx {
public static void main(String[] args) {
LinkedHashMap<String, Integer> wordcount =
new LinkedHashMap<String, Integer>();
try {
BufferedReader in = new BufferedReader(
new FileReader("c:\\test\\sample.txt"));
String str;
while ((str = in.readLine()) != null) {
str = str.toLowerCase(); // convert to lower case
int idx1 = -1;
for (int i = 0; i < str.length(); i++) {
if ((!Character.isLetter(str.charAt(i))) ||
(i + 1 == str.length())) {
if (i - idx1 > 1) {
if (Character.isLetter(str.charAt(i)))
i++;
String word = str.substring(idx1 + 1, i);
if (wordcount.containsKey(word)) {
wordcount.put(word, wordcount.get(word) + 1);
} else {
wordcount.put(word, 1);
}
}
idx1 = i;
}
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
ArrayList<Integer> values = new ArrayList<Integer>();
values.addAll(wordcount.values());
Collections.sort(values, Collections.reverseOrder());
int last_i = -1;
for (Integer i : values) {
if (last_i == i) // without duplicates
continue;
last_i = i;
for (String s : wordcount.keySet()) {
if (wordcount.get(s) == i) // which have this value
System.out.println(s + ":" + i);
}
}
}
}
Re: pls help me with word occurances program
I just answered this question on the other forum where you posted it.
http://www.javaprogrammingforums.com...s-program.html
Re: pls help me with word occurances program
@Norm
but u did not tell me how to remove top 10 elements. if u can give the code snippet to remove top 10 words ur help would be highly appreciated
Re: pls help me with word occurances program
Given a sorted list, look at the list class's API doc for methods you could use to remove items from the list.
Re: pls help me with word occurances program
there we have methods to get first,last, moiddle elements and so on none of it allows us to specify a range of elements like element 1 to element 10:(
Re: pls help me with word occurances program
Quote:
we have methods to get ...
What class are you using?
If you can get the first, do it 10 times.
Re: pls help me with word occurances program
Please do not post duplicate threads. I have locked your other thread and moved this one to the appropriate location.