import java.io.*; import java.util.*; public class SpellCheckerTester { protected SpellChecker spellChecker; /** * Initializes this SpellCheckerTester object. * */ public SpellCheckerTester () { spellChecker = new SpellChecker(); } // default constructor /** * Reads in and save a file of a specified type (“dictionary” or “document”). * The worstTime(t) is O(t log t), where t is the number of lines in the file. * * @param fileType - a String object representing the type of file, * “dictionary” or “document”, to be read in. * */ public void readFile (String fileType) { final String FILE_PROMPT = "\nPlease enter the path for the " + fileType + " file: "; final String DICTIONARY = "dictionary"; BufferedReader keyboardReader = new BufferedReader (new InputStreamReader (System.in)), fileReader; String filePath, line; boolean pathOK = false; while (!pathOK) { try { System.out.print (FILE_PROMPT); filePath = keyboardReader.readLine(); fileReader = new BufferedReader (new FileReader (filePath)); pathOK = true; while (true) { line = fileReader.readLine(); if (line == null) break; if (fileType.equals (DICTIONARY)) spellChecker.addToDictionarySet (line); else spellChecker.addToDocumentSet (line); } // while not at end of file } // try catch (IOException e) { System.out.println (e); } // catch IOException } // while } // method readFile /** * Prints the misspelled words - those that are in the document set but not * in the dictionary set. * The worstTime(m, n) is O(m log n), where m is the number of words in * the document set and n is the number of words in the dictionary set. * */ public void printResults() { final String ALL_CORRECT = "\n\nAll the words are spelled correctly."; final String MISSPELLED = "\n\nThe following words are misspelled:"; LinkedList misspelled = spellChecker.compare(); if (misspelled == null) System.out.println (ALL_CORRECT); else System.out.println (MISSPELLED + misspelled); } // method printResults } // class SpellCheckerTester