Skip to content

Commit 722fe2d

Browse files
Jessica CampbellJessica Campbell
Jessica Campbell
authored and
Jessica Campbell
committed
finally done
1 parent 6d087c3 commit 722fe2d

File tree

6 files changed

+2128
-3497
lines changed

6 files changed

+2128
-3497
lines changed

src/main/java/io/zipcoder/WC.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22

33
import java.io.FileNotFoundException;
44
import java.io.FileReader;
5-
import java.util.Map;
6-
import java.util.Iterator;
7-
import java.util.LinkedHashMap;
8-
import java.util.Scanner;
9-
import static java.util.stream.Collectors.toMap;
5+
import java.util.*;
106

117
public class WC {
12-
8+
139
private Iterator<String> si;
1410
private Map <String, Integer> wordsWithCounts = new LinkedHashMap<>();
1511

@@ -23,30 +19,47 @@ public WC(String fileName) {
2319
}
2420

2521
public WC(Iterator<String> si) {
22+
this.wordsWithCounts = new LinkedHashMap<String, Integer>();
2623
this.si = si;
2724
}
2825

29-
public void wordCount() {
26+
public Map<String, Integer> readWords() {
27+
3028
while (si.hasNext()) {
31-
String current = si.next().toLowerCase().replaceAll("[a-zñáéíóú]", ""); // deletes all other special characters and replaces them w blank space
29+
String current = si.next().toLowerCase().replaceAll("[^a-zñáéíóú]", ""); // deletes all other special characters and replaces them w blank space
3230
if (wordsWithCounts.containsKey(current)) {
3331
wordsWithCounts.put(current, wordsWithCounts.get(current) + 1); // if we have this word -> get the int value at that key and add one
34-
} else {
32+
} else
3533
wordsWithCounts.put(current, 1); // if we do not have the word, add it and set the count to one
36-
}
3734
}
35+
return wordsWithCounts;
3836
}
3937

40-
public void print(){
38+
public void reverseOrder(){
39+
// Map<String, Integer> sorted = readWords().entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
40+
// .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
41+
// return sorted;
4142
this.wordsWithCounts.entrySet()
4243
.stream()
4344
.sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
4445
.forEach(System.out::println);
4546
}
4647

47-
public static void main (String[] args){
48-
WC wc = new WC("/Users/jessicacampbell/Dev/EncapsulativeCharacters/src/main/resources/DonQuijote.txt");
49-
wc.print();
50-
}
48+
// public String display(){
49+
// readWords();
50+
// reverseOrder();
51+
// StringBuilder sb = new StringBuilder();
52+
// for(String key : wordsWithCounts.keySet()){
53+
// sb.append("\nWord: " + key + "\nCount:" + wordsWithCounts.get(key) + "\n__________");
54+
// }
55+
// System.out.println(sb.toString());
56+
// return sb.toString();
57+
// }
58+
59+
public static void main(String[] args) {
60+
WC wc = new WC("/Users/jessicacampbell/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/DonQuijote.txt");
61+
wc.readWords();
62+
wc.reverseOrder();
63+
}
5164

5265
}

0 commit comments

Comments
 (0)