// Allison Obourn // CSE 143 - lecture 11 // Counts the occurrences of words in a large file and prints the most common ones. // It demonstrates the use of a Map collection. import java.io.*; import java.util.*; public class WordCount2 { public static void main(String[] args) throws FileNotFoundException { System.out.println("Reading file..."); Scanner input = new Scanner(new File("mobydick.txt")); Map words = new TreeMap(); while(input.hasNext()) { String word = input.next().toLowerCase(); // when we get the value of a key that's not there, // we get null! Adding 1 to null causes an exception. // we therefore need to separate the cases where the key is already // in the map from the one where it's not. if(words.containsKey(word)) { words.put(word, words.get(word) + 1); } else { words.put(word, 1); } } // iterate over the map using the set of keys for(String word : words.keySet()) { System.out.println(word + " " + words.get(word)); } } }