CHU1TA26 vor 6 Jahren
Ursprung
Commit
4622b5ef1f

+ 6
- 2
src/main/java/io/zipcoder/Main.java Datei anzeigen

@@ -1,10 +1,14 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.Map;
4
+import java.util.stream.Stream;
5
+
3 6
 public class Main {
4 7
     public static void main(String[] args) {
5 8
 
6 9
         WC wc = new WC(WC.class.getResource("/someTextFile.txt").getFile());
7
-        wc.reverseAndPrint("/someTextFile.txt");
8
-
10
+        wc.wordCount();
11
+        Stream<Map.Entry<String, Integer>> entries = wc.reverse("/someTextFile.txt");
12
+        entries.forEach(entry -> System.out.println (String.format("%15d %s\n  ", entry.getValue(), entry.getKey() )));
9 13
     }
10 14
 }

+ 17
- 18
src/main/java/io/zipcoder/WC.java Datei anzeigen

@@ -6,6 +6,7 @@ import java.util.HashMap;
6 6
 import java.util.Iterator;
7 7
 import java.util.Map;
8 8
 import java.util.Scanner;
9
+import java.util.stream.Stream;
9 10
 
10 11
 public class WC {
11 12
     private Iterator<String> si;
@@ -26,30 +27,28 @@ public class WC {
26 27
         this.si = si;
27 28
     }
28 29
 
29
-    public void  wordCount(){
30
-    String words;
31
-       
32
-        while (si.hasNext()){
33
-            String lowerCaseWords =si.next().toLowerCase();
34
-             words = lowerCaseWords.replaceAll("[^a-z]", "");
30
+    public void  wordCount() {
31
+        String words;
32
+int count=0;
35 33
 
36
-            if (countTheWords.containsKey(words)){
37
-                       countTheWords.put(words, countTheWords.get(words) + 1 );
38
-                   } else{
39
-                       countTheWords.put(words, 1);
34
+        while (si.hasNext()) {
35
+            String lowerCaseWords = si.next().toLowerCase();
36
+            words = lowerCaseWords.replaceAll("[^a-z]" ,"");
40 37
 
41
-                   }
38
+            if (countTheWords.containsValue(words)) {
39
+                countTheWords.put(words, countTheWords.get(words) + 1 );
40
+            } else {
41
+                countTheWords.put( words, count++);
42
+
43
+            }
42 44
         }
43
-        countTheWords.entrySet().stream().
44
-                sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
45 45
 
46
-                .forEach (entry -> System.out.println (String.format("%15s  , %d\n  ", entry.getValue(), entry.getKey() )));
47 46
     }
48
-    public  void reverseAndPrint(String fileName){
49
-        countTheWords.entrySet().stream().
50
-                sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
47
+    public Stream<Map.Entry<String, Integer>> reverse(String fileName){
48
+        return countTheWords.entrySet().stream()
49
+                .sorted(Map.Entry.<String, Integer>comparingByValue().reversed());
50
+
51 51
 
52
-                .forEach (entry -> System.out.println (String.format("%15s  , %d\n  ", entry.getValue(), entry.getKey() )));
53 52
 
54 53
     }
55 54
 

+ 1
- 1
src/main/resources/someTextFile.txt Datei anzeigen

@@ -94,7 +94,7 @@ right."
94 94
 
95 95
 "But you knew that before," pleaded the man. "Besides--"
96 96
 
97
-"Yes, but I can't do it. He was there! His eyes spoke--I--don't touch
97
+"Yes, but I can't do it. He was there! His eyes spoke I don't touch
98 98
 me," she said; "I'm going back to my husband. Don't follow."
99 99
 
100 100
 

+ 0
- 5
src/test/java/io/zipcoder/WCTest.java Datei anzeigen

@@ -8,11 +8,6 @@ import java.util.Arrays;
8 8
 
9 9
 public class WCTest {
10 10
 
11
-    @Test
12
-    public void wordCount() {
13
-        WC wc = new WC("/someTextFile.txt");
14
-        wc.wordCount();
15 11
 
16
-    }
17 12
 
18 13
 }