i want to write a program that displays for example 5 most repeated word in a text. the words of the text saved in a map that its keys are words and its values are number of repeating that word.
this program displays the most repeated word but i don't know how to improve that to display 5 most repeated word(and how to use map instead of list).
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
public class MostRepeatedWord {
public static void main(String[] args) throws Exception {
String line, word = "";
int count = 0, maxCount = 0;
ArrayList<String> words = new ArrayList<String>();
//Opens file in read mode
FileReader file = new FileReader("data.txt ");
BufferedReader br = new BufferedReader(file);
//Reads each line
while((line = br.readLine()) != null) {
String string[] = line.toLowerCase().split("([,.\\s]+) ");
//Adding all words generated in previous step into words
for(String s : string){
words.add(s);
}
}
//Determine the most repeated word in a file
for(int i = 0; i < words.size(); i++){
count = 1;
//Count each word in the file and store it in variable count
for(int j = i+1; j < words.size(); j++){
if(words.get(i).equals(words.get(j))){
count++;
}
}
//If maxCount is less than count then store value of count in maxCount
//and corresponding word to variable word
if(count > maxCount){
maxCount = count;
word = words.get(i);
}
}
System.out.println("Most repeated word: " + word);
br.close();
}
}