import java.util.*; import java.io.*; // Determines word counts in a file. public class WordCount2 { public static void main(String[] args) throws FileNotFoundException { Scanner console = new Scanner(System.in); System.out.print("What is the name of the text file? "); String fileName = console.nextLine(); Scanner input = new Scanner(new File(fileName)); long start = System.currentTimeMillis(); Map counts = new HashMap(); while (input.hasNext()) { String word = input.next().toLowerCase(); if (counts.containsKey(word)) { counts.put(word, counts.get(word) + 1); } else { counts.put(word, 1); } } long end = System.currentTimeMillis(); long elapsed = end - start; // Print out words with over 1000 occurrences for (String word : counts.keySet()) { int count = counts.get(word); if (count > 1000) { System.out.println(count + "\t" + word); } } System.out.println("The file has " + counts.size() + " words."); System.out.println("Took " + elapsed + " ms."); } }