我一直在学习并提出一个问题。我尝试从文本文件中搜索特定的姓名和员工编号。
我尝试在网上进行研究,但没有发现特别明显的结果。

如何解决此“找不到符号”问题并使其正常工作?
我收到错误提示,


  。\ txtFileReader.java:15:错误:找不到符号
          while((line = filescan.readLine())!= null)
                                ^符号:方法readLine()位置:扫描仪1类型的变量文件错误


我的代码是

import java.util.*;
import java.io.*;

public class txtFileReader
{
    private String words;
    private Scanner typescan, filescan;

    public void run() throws IOException
    {
        filescan = new BufferedReader(new FileReader("EmpInfo.txt"));
        String line = "";
        words = typescan.nextLine();
        while((line = filescan.readLine()) != null)
        {
            if(line.matches(words))
            {
                System.out.print(line);
                break;
            }
            else
            {
                System.out.print("Sorry, could not find it.");
                break;
            }
        }
    }
}


更新:

我添加了“ BufferedReader filescan”部分,而不是使用“ filescan”
仍然在编译后收到另一个错误“ NullPointerException”

Exception in thread "main" java.lang.NullPointerException
        at txtFileReader.run(txtFileReader.java:15)
        at Main.main(Main.java:9)


...

  Public void run() throws IOException
        {
        BufferedReader filescan = new BufferedReader(new FileReader("EmpInfo.txt"));
        String line = "";
        words = typescan.nextLine();


...

更新2:

它仍然显示NullPointerException问题。

Exception in thread "main" java.lang.NullPointerException
        at txtFileReader.run(txtFileReader.java:15)
        at Main.main(Main.java:9)


我不确定,但我假设由于文本文件有问题要阅读,它给NullPointerException吗?

最佳答案

filescan更改为BufferedReader

BufferedReader filescan;


更新:

因为未初始化NullPointerException,所以抛出typescan

String words = "Something";
Scanner typescan; // Not used
BufferedReader filescan;

filescan = new BufferedReader(new FileReader("EmpInfo.txt"));
String line = "";
//words = typescan.nextLine(); // NullPointerException otherwise
while((line = filescan.readLine()) != null) {
    //if(line.matches(words)) { // What is this?
    if(line.equals(words)) {
        System.out.print(line);
        break;
    }
}

10-06 01:01