exo5: Add the code that permit to build a word counter.
[Project_POO.git] / exo5 / Main.java
CommitLineData
437984e3
JB
1import java.io.File;
2import java.util.Scanner;
3import java.io.IOException;
4import java.util.TreeMap;
2260b7d6
JB
5
6class Main {
7
8 /**
9 * The main() function
10 * @param String[] args main() function arguments array
11 */
12 public static void main(String[] args) {
437984e3
JB
13 TreeMap<String, Integer> tm = new TreeMap<String, Integer>();
14 File f = new File("test_file.txt");
15 try {
16 Scanner sc = new Scanner(f);
17 while (sc.hasNext()) {
18 String wordCursor = sc.next();
19 //NOTE: words comparaison is case sensitive and punctuation aware
20 Integer count = tm.get(wordCursor);
21 if (count == null) {
22 count = 0;
23 }
24 tm.put(wordCursor, count + 1);
25 }
26 sc.close();
27 }
28 catch (IOException e) {
29 e.printStackTrace();
30 }
31 for (String wordCursor : tm.keySet()) {
32 System.out.println("Word \"" + wordCursor + "\" occured " + tm.get(wordCursor) + " times");
33 }
2260b7d6
JB
34 }
35}