X-Git-Url: https://git.piment-noir.org/?p=Project_POO.git;a=blobdiff_plain;f=exo5%2FMain.java;h=953bdfe46f61bf06b962e85745c9feeacae93d06;hp=4075cbc4ed5997f2f9c18bf6b6ec1b59a5fba719;hb=437984e373831a86f42d66385270939a9d72f29b;hpb=229c68282dc8bc3cbd9b8c6a37f6fa1dcd378da0 diff --git a/exo5/Main.java b/exo5/Main.java index 4075cbc..953bdfe 100644 --- a/exo5/Main.java +++ b/exo5/Main.java @@ -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 tm = new TreeMap(); + 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"); + } } }