Commit 538ff8c1 authored by Adam Blank's avatar Adam Blank
Browse files

Add new file

parent 15942dd8
Showing with 33 additions and 0 deletions
+33 -0
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class MostCommonWord {
private static final String BOOK_FILENAME = "alice.txt";
public static void main(String[] args) throws FileNotFoundException {
// NOTE: This is wrong...do not copy.
// keys are words
// values are number of times that word occurs
Map<String, Integer> map = new HashMap<>();
Scanner s = new Scanner(new File(BOOK_FILENAME));
while (s.hasNext()) {
String next = s.next();
if (!map.containsKey(next)) {
map.put(next, 0);
}
int oldValue = map.get(next);
map.put(next, oldValue + 1);
// map.get(next) = map.get(next) + 1
}
// print size of map
String best = "???";
for (String key : map.keySet()) {
if (map.get(best) == null || map.get(key) > map.get(best)) {
best = key;
}
// ... implement together
}
System.out.println(best);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment