1  import java.util.HashSet;
  2  import java.util.Scanner;
  3  import java.util.Set;
  4  import java.io.File;
  5  import java.io.FileNotFoundException;
  6  
  7  /**
  8     This program checks which words in a file are not present in a dictionary.
  9  */
 10  public class SpellCheck
 11  {
 12     public static void main(String[] args) 
 13        throws FileNotFoundException
 14     {
 15        // Read the dictionary and the document
 16  
 17        Set<String> dictionaryWords = readWords("words");
 18        Set<String> documentWords = readWords("alice30.txt");
 19  
 20        // Print all words that are in the document but not the dictionary
 21  
 22        for (String word : documentWords)
 23        {
 24           if (!dictionaryWords.contains(word))
 25           {
 26              System.out.println(word);
 27           }
 28        }
 29     }
 30  
 31     /**
 32        Reads all words from a file.
 33        @param filename the name of the file
 34        @return a set with all lowercased words in the file. Here, a 
 35        word is a sequence of upper- and lowercase letters.
 36     */
 37     public static Set<String> readWords(String filename)
 38        throws FileNotFoundException
 39     {
 40        Set<String> words = new HashSet<String>();
 41        Scanner in = new Scanner(new File(filename));
 42        // Use any characters other than a-z or A-Z as delimiters
 43        in.useDelimiter("[^a-zA-Z]+");
 44        while (in.hasNext())
 45        {
 46           words.add(in.next().toLowerCase());        
 47        }
 48        return words;
 49     }
 50  }