-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmost repeated word.java
More file actions
39 lines (32 loc) · 1.25 KB
/
most repeated word.java
File metadata and controls
39 lines (32 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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>();
FileReader file = new FileReader("data.txt ");
BufferedReader br = new BufferedReader(file);
while((line = br.readLine()) != null) {
String string[] = line.toLowerCase().split("([,.\\s]+) ");
for(String s : string){
words.add(s);
}
}
for(int i = 0; i < words.size(); i++){
count = 1;
for(int j = i+1; j < words.size(); j++){
if(words.get(i).equals(words.get(j))){
count++;
}
}
if(count > maxCount){
maxCount = count;
word = words.get(i);
}
}
System.out.println("Most repeated word: " + word);
br.close();
}
}