/* * Dictionary.java * * Created: April 9, 2005 * By: Stan Sclaroff, Boston University Dept. of Computer Science * Description: Dictionary class for program assignment 7. * * public method: boolean findWord(String s) * returns true if word is in dictionary * false otherwise * * A private helper method loadWords() reads words for the * dictionary from the text file "words". * * NOTE: * You must specify the correct file path in the constructor for FileReader! * This path would be the place where your copy of the file "words" is located. * * Each word is stored as a string in a Vector object. * */ package program7; import java.io.*; import java.util.*; public class Dictionary { // word array initialized only once! private static Vector word = new Vector(); // Method returns true if the string is in the dictionary public static boolean findWord(String s){ if(word.isEmpty()) loadWords(); return word.contains(s.toLowerCase()); } private static void loadWords(){ // open file String line; try{ BufferedReader inputStream = new BufferedReader(new FileReader("C:\\Program7\\src\\program7\\words")); while((line = inputStream.readLine()) != null) if(line.length() > 1) word.addElement(line.toLowerCase()); inputStream.close(); } catch(FileNotFoundException e){ System.out.println("Cannot find file \"words\""); System.exit(0); } catch(IOException e){ System.out.println("Problem in reading input from file \"words\""); System.exit(0); } } }