// Hunter Schafer, CSE 143 // This program counts the number of words in a large file. // It demonstrates the use of a Set collection. import java.io.*; import java.util.*; public class WordCount { public static void main(String[] args) throws FileNotFoundException { System.out.println("Reading file..."); //Scanner input = new Scanner(new File("smallmoby.txt")); Scanner input = new Scanner(new File("mobydick.txt")); long start = System.currentTimeMillis(); int uniqueWords = countUnique(input); printWordCounts(input); long end = System.currentTimeMillis(); long elapsed = end - start; System.out.println("The file has " + uniqueWords + " words."); System.out.println("Took " + elapsed + " ms."); } public static int countUnique(Scanner input) { //List words = new LinkedList(); Set words = new HashSet(); while (input.hasNext()) { String word = input.next(); //if (!words.contains(word)) { words.add(word); // } } // Can't do this :( // for (int i = 0; i < words.size(); i++) { for (String word : words) { System.out.println(word); } return words.size(); } public static void printWordCounts(Scanner input) { Map wordCounts = new TreeMap(); while (input.hasNext()) { String word = input.next(); // "flew" if (!wordCounts.containsKey(word)) { wordCounts.put(word, 1); } else { int count = wordCounts.get(word); // 1 wordCounts.put(word, count + 1); } } for (String key : wordCounts.keySet()) { System.out.println(key + " " + wordCounts.get(key)); } } }