-
Notifications
You must be signed in to change notification settings - Fork 828
/
Copy pathMostRepeatedWord.java
52 lines (46 loc) · 1.16 KB
/
MostRepeatedWord.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
package misc;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class MostRepeatedWord {
public static void main(String[] args) {
String line, word = "";
int count = 0, maxCount = 0;
ArrayList<String> words = new ArrayList<String>();
// Opens file in read mode
FileReader file;
try {
file = new FileReader("file.txt");
BufferedReader br = new BufferedReader(file);
// Reads each line
try {
while ((line = br.readLine()) != null) {
String string[] = line.toLowerCase().split("([,.\\s]+)");
for (String s : string) {
words.add(s);
}
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Determine the most repeated word in a file
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 words: " + word);
}
}