public class WordCountTest { public static void main(String[] args) throws Exception { WordReader reader = new WordReader(new InputStreamReader(System.in)); Map wordMap = new TreeMap(); String word; while ((word=reader.readWord()) != null) { Integer count = wordMap.containsKey(word) ? wordMap.get(word) : 0; wordMap.put(word, ++count); } // Print out using the natural order of the key for (Map.Entry entry : wordMap.entrySet()) { System.out.println(entry.getKey()+" : "+entry.getValue()); } } }