exo5: Add the code that permit to build a word counter.
[Project_POO.git] / exo5 / Main.java
index 4075cbc4ed5997f2f9c18bf6b6ec1b59a5fba719..953bdfe46f61bf06b962e85745c9feeacae93d06 100644 (file)
@@ -1,3 +1,7 @@
+import java.io.File;
+import java.util.Scanner;
+import java.io.IOException;
+import java.util.TreeMap;
 
 class Main {
 
@@ -6,6 +10,26 @@ class Main {
      * @param String[] args main() function arguments array
      */
     public static void main(String[] args) {
-
+        TreeMap<String, Integer> tm = new TreeMap<String, Integer>();
+        File f = new File("test_file.txt");
+        try {
+            Scanner sc = new Scanner(f);
+            while (sc.hasNext()) {
+                String wordCursor = sc.next();
+                //NOTE: words comparaison is case sensitive and punctuation aware
+                Integer count = tm.get(wordCursor);
+                if (count == null) {
+                    count = 0;
+                }
+                tm.put(wordCursor, count + 1);
+            }
+            sc.close();
+        }
+        catch (IOException e) {
+            e.printStackTrace();
+        }
+        for (String wordCursor : tm.keySet()) {
+            System.out.println("Word \"" + wordCursor + "\" occured " + tm.get(wordCursor) + " times");
+        }
     }
 }