import java.util.*; import java.io.*; // Determines the unique words in a file. public class WordCount { 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(); Set words = new TreeSet(); while (input.hasNext()) { String word = input.next(); words.add(word); } long end = System.currentTimeMillis(); long elapsed = end - start; System.out.println("The file has " + words.size() + " words."); System.out.println("Took " + elapsed + " ms."); // for-each loop for (String s : words) { System.out.println(s); } } }