You can roll your own spell checker against whatever word (dictionary) text file you want, for example:
/**
* Checks the supplied word against the words contained within the supplied
* word (dictionary) text file. Each line within a word (dict) file must be
* a single word. This method is not letter case sensitive.<br><br>
*
* @param dictPath (String) the path and file name to the Word file.<br>
*
* @param wordToCheck (String) The word to check for spelling.<br>
*
* @return (boolean) True is returned if the supplied word is contained
* within the supplied word file. False is returned if it is not.
*/
public static boolean spellCheck(String dictPath, String wordToCheck) {
boolean res = false;
java.nio.file.Path path = java.nio.file.Paths.get(dictPath);
try (java.io.BufferedReader reader = java.nio.file.Files.newBufferedReader(path)) {
String str;
while ((str = reader.readLine()) != null) {
if (str.trim().equalsIgnoreCase(wordToCheck)) {
res = true;
break;
}
}
} catch (java.io.IOException e) {
e.printStackTrace();
}
return res;
}
How you might use this method within your console application:
java.util.Scanner userInput = new java.util.Scanner(System.in);
String dictionaryFilePath = "WordFile.txt";
String inputString = "";
while (inputString.isEmpty()) {
System.out.println();
System.out.println("Enter a String of any length:");
System.out.print("Entry: --> ");
inputString = userInput.nextLine().trim();
}
String[] words = inputString.split("\\s+");
java.util.List<String> misspelledWords = new java.util.ArrayList<>();
for (int i = 0; i < words.length; i++) {
if (!spellCheck(dictionaryFilePath, words[i])) {
misspelledWords.add(words[i]);
}
}
if (!misspelledWords.isEmpty()) {
System.out.println();
System.out.println("You misspelled the following words or these");
System.out.println("words were not within the dictionary:");
System.out.println("============================================");
for (String wrds : misspelledWords) {
System.out.print(wrds + "\t");
}
}
else {
System.out.println("Good for you...No misspelled words!");
}
i
and yourj
… (also your logic - think about what the loops you’ve written mean)for
loop, try thinking around itanswer.charAt(i)
use ofi
as index ofanswer
is wrong ~