import java.util.HashMap; import java.util.Scanner; import java.util.Map.Entry; public class WordCount { /** * @param args */ public static void main(String[] args) { Scanner in = new Scanner(System.in); boolean done = !(in.hasNext()); //create a HashMap: keys are Strings, values are Integers HashMap map = new HashMap(); while(!done){ String word = in.next(); if(word.equals(".")){ done = true; } else { //check if we have seen it if(map.containsKey(word)){ //if so, add one to its frequency int count = map.get(word); map.put(word, count+1); } else { //if not, add it with the value 1 map.put(word, 1); } } } for(Entry e: map.entrySet()){ //print out each entry System.out.println(e); } } }