Lauren Green 6 年前
父节点
当前提交
85b550b316

+ 31
- 2
src/main/java/io/zipcoder/WC.java 查看文件

2
 
2
 
3
 import java.io.FileNotFoundException;
3
 import java.io.FileNotFoundException;
4
 import java.io.FileReader;
4
 import java.io.FileReader;
5
-import java.util.Iterator;
6
-import java.util.Scanner;
5
+import java.util.*;
7
 
6
 
8
 public class WC {
7
 public class WC {
8
+    private final List<String> list;
9
     private Iterator<String> si;
9
     private Iterator<String> si;
10
 
10
 
11
     public WC(String fileName) {
11
     public WC(String fileName) {
19
 
19
 
20
     public WC(Iterator<String> si) {
20
     public WC(Iterator<String> si) {
21
         this.si = si;
21
         this.si = si;
22
+
23
+        while(si.hasNext()) {
24
+            list.add(si.next());
25
+        }
26
+    }
27
+
28
+
29
+    public HashMap<String, Integer> countTheWords (String input) {
30
+        input.toLowerCase();
31
+        String[] words = input.split("[\\p{Punct}\\s]+");
32
+        System.out.println(words);
33
+        HashMap<String, Integer> wordCount = new HashMap<String, Integer>();
34
+
35
+        for(String word: words) {
36
+            if(wordCount.get(word) == null) {
37
+                wordCount.put(word, 1);
38
+            } else {
39
+                int value = wordCount.get(word);
40
+                value++;
41
+                wordCount.put(word, value);
42
+            }
43
+        }
44
+
45
+        return wordCount;
46
+    }
47
+
48
+    public Iterator<String> getSi() {
49
+
50
+        return si;
22
     }
51
     }
23
 }
52
 }

+ 7906
- 0
src/main/resources/DoublePlay.txt
文件差异内容过多而无法显示
查看文件


+ 1
- 0
src/main/resources/someTextFile.txt 查看文件

1
+Red cow! Yellow cow, blue red cow. Duck!

+ 13
- 0
src/test/java/io/zipcoder/WCTest.java 查看文件

8
 
8
 
9
 public class WCTest {
9
 public class WCTest {
10
 
10
 
11
+    @Test
12
+    public void testCountTheWords() {
13
+        //Given
14
+        WC test = new WC(WC.class.getResource("/someTextFile.txt").getFile());
15
+        System.out.println(test.getSi().next());
16
+
17
+
18
+        //When
19
+
20
+        //Then
21
+
22
+    }
23
+
11
 }
24
 }