问题是我正在尝试制作的拼写检查器。我有一个字典文件,其中包含大量要与用户输入进行比较的单词,因此它可以检测任何可能的拼写错误。我的问题是,无论您键入什么,它总是会说拼写不正确,而实际上拼写不正确。是否有任何解决方案或更好的方法来检测用户输入的销售错误。
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
正如你所看到的,即使我的拼写是正确的,它也会给出一个拼写不正确的错误。任何帮助都会很棒,并提前感谢您。
皈依舞
相关分类