-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqb271.java
38 lines (32 loc) · 1.07 KB
/
qb271.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
import java.util.*;
import java.io.*;
public class QB271 {
public static void main(String[] args) throws Exception {
FileWriter fw = new FileWriter("inputData.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Hi Hello How are you Hi");
bw.newLine();
bw.write("Hello I am fine How are you");
bw.write(12);
bw.close();
FileReader fr = new FileReader("inputData.txt");
BufferedReader br = new BufferedReader(fr);
HashMap<String, Integer> map = new HashMap<>();
String line = "";
while ((line = br.readLine()) != null) {
String words[] = line.split(" ");
for (String w : words) {
if (map.get(w) == null) {
map.put(w, 1);
} else {
map.put(w, (map.get(w) + 1));
}
}
}
for (Map.Entry e : map.entrySet()) {
System.out.println("Count for: " + e.getKey() + " is = " + e.getValue());
}
br.close();
fr.close();
}
}