使用Java的BufferedReader和Scanner解析文本文件。

huangapple go评论71阅读模式
英文:

Parsing a text file using java bufferedReader and Scanner

问题

我有以下的配置文件,我正在尝试使用它来设置程序中的全局变量。

OUTPUT_FILE_PATH = filename2.txt
MAX_CRAWL_DEPTH = 2
NUMBER OF CRAWLERS = 10
SEED_URL = https://www.lehigh.edu/home
DOMAIN = lehigh.edu
DELAY = 10

为了解析这个文本文件,我使用了以下代码:

String line = "";
BufferedReader buffRead = new BufferedReader(new FileReader(CONFIG_FILE_PATH));
Scanner charlie = new Scanner(line);

String varName;
while ((line = buffRead.readLine()) != null){
    System.out.println(line);
    charlie.useDelimiter(" = ");
    varName = charlie.next();
    System.out.println(varName);

    if (varName == "OUTPUT_FILE_PATH")
        OUTPUT_FILE_PATH = charlie.next();
    else if (varName == "MAX_CRAWL_DEPTH")
        MAX_CRAWL_DEPTH = charlie.nextInt();
    else if (varName == "NUMBER_OF_CRAWLERS")
        NUMBER_OF_CRAWLERS = charlie.nextInt();
    else if (varName == "SEED_URL")
        SEED_URL = charlie.next();
    else if (varName == "DOMAIN")
        DOMAIN = charlie.next();
    else if (varName == "DELAY")
        delay = charlie.nextInt();
}

在运行代码时,我得到了如下输出:

OUTPUT_FILE_PATH = filename2.txt
Exception in thread "main" java.util.NoSuchElementException
        at java.base/java.util.Scanner.throwFor(Scanner.java:937)
        at java.base/java.util.Scanner.next(Scanner.java:1478)
        at Globals.setGlobals(Globals.java:44)

第 44 行是我设置 varName = charlie.next(); 的地方。有什么可能导致这样的错误吗?还有关于如何解析这个文件的其他提示吗?我觉得我的 if 语句可能不是最佳的处理方法。

英文:

I have the following config file that I am trying to use to set global variables in my program

OUTPUT_FILE_PATH = filename2.txt
MAX_CRAWL_DEPTH = 2
NUMBER OF CRAWLERS = 10
SEED_URL = https://www.lehigh.edu/home
DOMAIN = lehigh.edu
DELAY = 10

To parse this text file I am using this

String line = "";
            BufferedReader buffRead = new BufferedReader(new FileReader((CONFIG_FILE_PATH)));
            Scanner charlie = new Scanner(line);

            String varName;
            while ((line = buffRead.readLine()) != null){
                System.out.println(line);
                charlie.useDelimiter(" = ");
                varName = charlie.next();
                System.out.println(varName);



                if (varName == "OUTPUT_FILE_PATH")
                    OUTPUT_FILE_PATH = charlie.next();
                else if (varName == "MAX_CRAWL_DEPTH")
                    MAX_CRAWL_DEPTH = charlie.nextInt();
                else if (varName == "NUMBER_OF_CRAWLERS")
                    NUMBER_OF_CRAWLERS = charlie.nextInt();
                else if (varName == "SEED_URL")
                    SEED_URL = charlie.next();
                else if (varName == "DOMAIN")
                    DOMAIN = charlie.next();
                else if (varName == "DELAY")
                    delay = charlie.nextInt();
            }

I get this output when running the code

OUTPUT_FILE_PATH = filename2.txt
Exception in thread "main" java.util.NoSuchElementException
        at java.base/java.util.Scanner.throwFor(Scanner.java:937)
        at java.base/java.util.Scanner.next(Scanner.java:1478)
        at Globals.setGlobals(Globals.java:44)

Line 44 is where I set varName = charlie.next();
Any reason why this would be wrong? Any other tips on how to go about parsing this file? I feel like my if statements is not the best way to do it.

答案1

得分: 1

抛出NoSuchElementException是因为您在此行中实例化扫描器时提供的字符串:

Scanner charlie = new Scanner(line);

实际上是:

Scanner charlie = new Scanner("");

因为在将line设置为""之后您从未修改过它。

您应该执行以下操作:

Scanner charlie = new Scanner(System.in);

另外,扫描器实例不接受字符串。

英文:

It's throwing the NoSuchElementException because the String you are providing in the instance of the scanner on this line:

Scanner charlie = new Scanner(line);

Is actually just:

Scanner charlie = new Scanner("");

Since you never modified line after you set it to "".

You should be doing the following:

Scanner charlie = new Scanner(System.in);

Also, a Scanner instance doesn't accept a String.

答案2

得分: 0

我需要在每次更新行之后在while循环内部定义扫描器,谢谢大家。

英文:

I needed to define the scanner inside the while loop after line got updated each time, thanks everyone

huangapple
  • 本文由 发表于 2020年10月19日 05:32:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64418555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定