Проблема в том, что моя проверка орфографии, которую я пытаюсь сделать.У меня есть файл словаря, который содержит большое количество слов для сравнения с пользовательским вводом, чтобы он мог обнаружить любые возможные орфографические ошибки.Проблема с моим заключается в том, что независимо от того, что вы печатаете, он всегда скажет, что написание неверно, когда это не так.Есть ли какое-либо решение или лучший метод для обнаружения ошибок продажи пользовательского ввода.
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class SpellChecker2 {
public static void main(String[] args) throws FileNotFoundException
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter a String");
String userWord = input.nextLine();
final String theDictionary = "dictionary.txt";
String[] words = dictionary(theDictionary);
boolean correctSpelling = checking(words, userWord);
if (!correctSpelling)
{
System.out.println("Incorrect spelling");
}
else
{
System.out.println("The spelling is correct");
}
}
public static String[] dictionary(String filename) throws FileNotFoundException
{
final String fileName = "dictionary.txt";
Scanner dictionary = new Scanner(new File(fileName));
int dictionaryLength =0;
while (dictionary.hasNext())
{
++dictionaryLength;
dictionary.nextLine();
}
String [] theWords = new String[dictionaryLength];
for ( int x = 0; x < theWords.length ; x++)
dictionary.close();
return theWords;
}
public static boolean checking(String[] dictionary, String userWord)
{
boolean correctSpelling = false;
for ( int i =0; i < dictionary.length; i++)
{
if (userWord.equals(dictionary[i]))
{
correctSpelling = true;
}
else
correctSpelling = false;
}
return correctSpelling;
}
}
Результат, который я получаю:
Please enter a String
hello
Incorrect spelling
Как вы можетеПосмотрите, даже если мое написание было правильным, оно дает ошибку, что написание было неправильным.Любая помощь будет отличной, и спасибо заранее.