// Hunter Schafer, CSE 143 // This program counts the number of unique words in a large file. // This program calculates the number of unique words in a file // and prints out information about how long it took to run import java.io.*; import java.util.*; public class UniqueWords { public static void main(String[] args) throws FileNotFoundException { System.out.println("Reading file..."); Scanner input = new Scanner(new File("mobydick.txt")); long start = System.currentTimeMillis(); int uniqueWords = countUnique(input); long end = System.currentTimeMillis(); long elapsed = end - start; System.out.println("The file has " + uniqueWords + " words."); System.out.println("Took " + elapsed + " ms."); } // pre: input is not null // post: returns the number of unique tokens in input public static int countUnique(Scanner input) { Set words = new HashSet(); while (input.hasNext()) { String word = input.next(); words.add(word); } // Wanted to remove all odd length words // /* Doesn't work for (String word : words) { if (word.length() % 2 == 1) { words.remove(word); } } */ // Iterator Interface // hasNext() // next() // remove // Iterable Interface // iterator() Iterator itr = words.iterator(); while (itr.hasNext()) { String word = itr.next(); if (word.length() % 2 == 1) { itr.remove(); } } return words.size(); } }