-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFindTheHighestRepeatingWordFromATextFile.java
58 lines (53 loc) · 2 KB
/
FindTheHighestRepeatingWordFromATextFile.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
53
54
55
56
57
58
import java.util.*;
import java.util.Map.Entry;
import java.io.*;
public class FindTheHighestRepeatingWordFromATextFile {
public static Map<String, Integer> buildWordMap(String fileName) {
Map<String, Integer> wordMap = new HashMap<>();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
String line = null;
while ((line = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, " ");
while(st.hasMoreTokens()) {
String word = st.nextToken().toLowerCase();
if (wordMap.containsKey(word)) {
wordMap.put(word, (wordMap.get(word) + 1));
} else {
wordMap.put(word, 1);
}
}
}
}
catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
}
return wordMap;
}
public static List<Entry<String, Integer>> sortByValue(Map<String, Integer> wordMap) {
Set<Entry<String, Integer>> entries = wordMap.entrySet();
List<Entry<String, Integer>> list = new ArrayList<>(entries);
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return (o2.getValue()).compareTo(o1.getValue());
}
});
return list;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String fileName = new String();
System.out.print("Please enter the path of the file that you want to read: ");
fileName = input.next();
Map<String, Integer> wordMap = buildWordMap(fileName);
List<Entry<String, Integer>> list = sortByValue(wordMap);
System.out.println();
System.out.println("List of repeated word from file and their count:");
for (Map.Entry<String, Integer> entry : list) {
if (entry.getValue() > 1) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
}