【问题标题】:How to compare an user input to a text file using BufferedReader如何使用 BufferedReader 将用户输入与文本文件进行比较
【发布时间】:2021-11-12 10:50:18
【问题描述】:

所以我正在开展一个项目,该项目需要我将用户输入与 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
}

【问题讨论】:

    标签: java text-files bufferedreader


    【解决方案1】:

    在方法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 的最后一行,在Lab5Program1 类中调用java.lang.System 类的方法exit。否则,每次运行 Lab5Program1 类时,都会启动一个不会终止的新 JVM。

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

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

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

    【讨论】:

      【解决方案2】:

      错误来自检查单词是否存在的函数。从文本文件中读取的每一行都没有使用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;
          } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多