1

配列を使用して標準の辞書ファイルから読み取るスペル チェックを実装するプログラムを作成していました。Array を使用するのはこれが初めてで、メソッドを呼び出す方法がよくわかりません。ですから、Eclipse は正しい単語と間違った単語の両方に対してどちらか一方の答えを与え続けたので、ブール法で間違いを犯したと思います。コードの修正を手伝ってくれる人はいますか?

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class CheckingWords {
public static void main(String[] args) throws FileNotFoundException
{

    Scanner input = new Scanner(System.in);
    System.out.println("Enter the word you would like to spell check"); 
    String userWord = input.nextLine();

    final String filename = "americanWords.txt";

    String[] words = dictionary(filename);
    boolean correctSpelling = checkWord(words, userWord);

if (correctSpelling)
    {
    System.out.println("That word is not correct");
    }
else 
    {
    System.out.println("That is the correct spelling");
    }

}

public static String[] dictionary(String filename) throws FileNotFoundException
{
    final String fileName = "americanWords.txt";

    Scanner dictionary = new Scanner(new File(fileName));
    int dictionaryLength =0;
    while (dictionary.hasNext())
    {
        ++dictionaryLength;
        dictionary.nextLine();
    }


    String [] words = new String[dictionaryLength];
        for ( int i = 0; i < words.length ; i++)


        dictionary.close();
    return words;
}

public static boolean checkWord(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;
}

}
4

3 に答える 3