In: Computer Science
ParseTxtFile.java:
Code:
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
public class MostRepeatedWord {
public static void main(String[] args) throws Exception {
String paragraph, word = "";
int sCount = 0, maxCount = 0;
ArrayList<String> wordsList = new
ArrayList<String>();
//Parsing Text File
FileReader file = new FileReader("inputFile.txt");
BufferedReader br = new BufferedReader(file);
// Each Line Iteration
while((paragraph = br.readLine()) != null) {
String string[] =
paragraph.toLowerCase().split("([,.\\s]+)");
for(String s : string){
wordsList.add(s);
}
}
// Finding repeated word
for(int i = 0; i < wordsList.size(); i++){
sCount = 1;
for(int j = i+1; j < wordsList.size(); j++){
if(wordsList.get(i).equals(wordsList.get(j))){
sCount++;
}
}
if(sCount > maxCount){
maxCount = sCount;
word = wordsList.get(i);
}
}
System.out.println("The most repeated word is: " + word);
System.out.println("No of Occurences: " + sCount);
br.close();
}
}
inputFile.txt:
Output:
The most repeated word is: Mobile
No of Occurences: 8