如何使用 BufferedReader 将用户输入与文本文件进行比较

How to compare an user input to a text file using BufferedReader

所以我正在做一个项目,需要我将用户输入与 txt 文件中的单词列表进行比较。我一直在尝试将作为字符串的输入与 BufferReader 进行比较,但它没有奏效。欢迎任何建议

这是项目的代码

public class Lab5Program1 {
    public static void main(String[] args) throws IOException {
        File file = new File("fileName");
        BufferedReader br = new BufferedReader(new FileReader(file));
        /** In order to read a text file that is inside the package, you need to call the actual file and then pass it
         * to the BufferedReader. So that it can be used in the file**/


//        String[] wordArray = { "hello", "goodbye", "cat", "dog", "red", "green", "sun", "moon" };
        String isOrIsNot, inputWord;

        // This line asks the user for input by popping out a single window
        // with text input
        inputWord = JOptionPane.showInputDialog(null, "Enter a word in all lower case:");

        // if the inputWord is contained within wordArray return true
        if (wordIsThere(inputWord, br))
            isOrIsNot = "is"; // set to is if the word is on the list
        else
            isOrIsNot = "is not"; // set to is not if the word is not on the list

        // Output to a JOptionPane window whether the word is on the list or not
        JOptionPane.showMessageDialog(null, "The word " + inputWord + " " + isOrIsNot + " on the list.");
    } //main

    public static boolean wordIsThere(String findMe, BufferedReader bufferedReader) throws IOException {

//        for (int i = 0; i < bufferedReader.lines() ; i++){
//            if (findMe.equals(theList[i])){
//                return true;
//            }
//        }

        while((findMe = bufferedReader.readLine()) != null) {
            if (findMe.equals(bufferedReader.readLine())){
                return true;
            }
        }
        return false;
    }  // wordIsThere
}

在方法wordIsThere中,参数findMe就是你要找的词。但是,您使用从文件中读取的行覆盖参数的值。

您应该声明一个单独的变量来存储您从文件中读取的文本行。

public static boolean wordIsThere(String findMe, BufferedReader bufferedReader) throws IOException {
    String line = bufferedReader.readLine(); // read first line of file
    while(line != null) {
        if (findMe.equals(line)){
            return true;
        }
        line = bufferedReader.readLine(); // read next line of file
    }
    return false;
}

另请注意,由于您正在使用 JOptionPane 获取用户输入,因此将启动一个单独的线程,并且该线程不会在方法 main 终止时终止。因此,您应该在 main、class Lab5Program1 的最后一行调用 class java.lang.System 的方法 exit。否则,每次 运行 class Lab5Program1 都会启动一个不会终止的新 JVM。

对于控制台应用程序,您可以使用 class java.util.Scanner 获取用户输入。

Scanner stdin = new Scanner(System.in);
System.out.print("Enter a word in all lower case: ");
String inputWord = stdin.nextLine();

还请考虑在完成文件后将其关闭。在您的情况下,没有必要,因为 JVM 终止时文件会自动关闭。

错误来自检查单词是否存在的函数。来自文本文件 reader 的每一行都没有被 findMe 检查。进行了这些更改,效果很好。

    public static boolean wordIsThere(String findMe, BufferedReader br) throws IOException {
        for (String word = br.readLine() ; word != null; word = br.readLine()) {
            if (word.equals(findMe))
                return true;
        }
        return false;
    }