import java.util.*; import java.io.*; public class SpellCheck { public static void main(String[] args) throws IOException { PrintStream out = System.out; if (args.length != 1) { out.println("Usage: java SpellCheck "); out.println("where is linked or array"); System.exit(1); } // create the dictionary list String listType = "ArrayList"; List dictionary = new ArrayList(); if (args[0].equals("linked")) { listType = "LinkedList"; dictionary = new LinkedList(); } // read in the dictionary using a scanner final String DICT_NAME = "wordsEn.txt"; Scanner dictionaryInput = new Scanner(new File(DICT_NAME)); while (dictionaryInput.hasNextLine()) { String word = dictionaryInput.nextLine().trim(); dictionary.add(word); } // read in the document Scanner textScanner = new Scanner(new File("AChristmasCarol.txt")); Set document = new TreeSet(); while (textScanner.hasNext()) { String text = textScanner.next(); text = text.replaceAll("-", " "); // replace all punctuation with space text = text.replaceAll("[\".,();:?!]", "").trim(); String[] words = text.split("\\s"); for (String word : words) { if (!word.isEmpty()) { document.add(word.toLowerCase()); } } } long startTime = System.nanoTime(); for (String word : document) { int idx = Collections.binarySearch(dictionary, word); if (idx < 0) { out.println(word + " is not in the dictionary"); } } long estimatedTime = System.nanoTime() - startTime; out.printf("Took %d ns to spellcheck the document using a %s%n%n", estimatedTime, listType); } }