import java.util.List; import java.util.ArrayList; import java.util.Scanner; import java.io.File; /** * Simple Dictionary class for storing and accessing words. * @author Dave Reed * @version 9/1/06 */ public class Dictionary { private List words; /** * Constructs an empty dictionary. */ public Dictionary() { words = new ArrayList(); } /** * Constructs a dictionary and initializes to store words from a file. * @param filename the name of the file of words */ public Dictionary(String filename) { this(); try { Scanner infile = new Scanner(new File(filename)); while (infile.hasNext()) { String nextWord = infile.next(); words.add(nextWord.toLowerCase()); } } catch (java.io.FileNotFoundException e) { System.out.println("FILE NOT FOUND"); } } /** * Adds a words to the dictionary. * @param newWord the word to be added */ public void add(String newWord) { words.add(newWord.toLowerCase()); } /** * Removes a word from the dictionary. * @param oldWord the word to be removed */ public void remove(String oldWord) { words.remove(oldWord.toLowerCase()); } /** * Dtermines whether a word is stored in the dictionary. * @param testWord the word to be searched for * @return true if testWord is stored; else false */ public boolean contains(String testWord) { return words.contains(testWord.toLowerCase()); } }