猿问

有没有办法让我的拼写检查器正常工作

问题是我正在尝试制作的拼写检查器。我有一个字典文件,其中包含大量要与用户输入进行比较的单词,因此它可以检测任何可能的拼写错误。我的问题是,无论您键入什么,它总是会说拼写不正确,而实际上拼写不正确。是否有任何解决方案或更好的方法来检测用户输入的销售错误。


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

正如你所看到的,即使我的拼写是正确的,它也会给出一个拼写不正确的错误。任何帮助都会很棒,并提前感谢您。


莫回无
浏览 99回答 1
1回答

皈依舞

是的。从checking上返回true。正如您现在所拥有的那样,只有最后一个单词匹配时,它才能为真。喜欢,public static boolean checking(String[] dictionary, String userWord) {&nbsp; &nbsp; for ( int i =0; i < dictionary.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (userWord.equals(dictionary[i])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return false;}此外,您需要dictionary通过向数组中添加单词来填充您的内容。而且,我更喜欢try-with-resources显式close()调用。就像是,public static String[] dictionary(String filename) throws FileNotFoundException {&nbsp; &nbsp; final String fileName = "dictionary.txt";&nbsp; &nbsp; int dictionaryLength = 0, i = 0;&nbsp; &nbsp; try (Scanner dictionary = new Scanner(new File(fileName))) {&nbsp; &nbsp; &nbsp; &nbsp; while (dictionary.hasNextLine()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++dictionaryLength;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dictionary.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; String[] theWords = new String[dictionaryLength];&nbsp; &nbsp; try (Scanner dictionary = new Scanner(new File(fileName))) {&nbsp; &nbsp; &nbsp; &nbsp; while (dictionary.hasNextLine()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; theWords[i] = dictionary.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return theWords;}
随时随地看视频慕课网APP

相关分类

Java
我要回答