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

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

Parsing a text file using java bufferedReader and Scanner

问题

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

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

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

  1. String line = "";
  2. BufferedReader buffRead = new BufferedReader(new FileReader(CONFIG_FILE_PATH));
  3. Scanner charlie = new Scanner(line);
  4. String varName;
  5. while ((line = buffRead.readLine()) != null){
  6. System.out.println(line);
  7. charlie.useDelimiter(" = ");
  8. varName = charlie.next();
  9. System.out.println(varName);
  10. if (varName == "OUTPUT_FILE_PATH")
  11. OUTPUT_FILE_PATH = charlie.next();
  12. else if (varName == "MAX_CRAWL_DEPTH")
  13. MAX_CRAWL_DEPTH = charlie.nextInt();
  14. else if (varName == "NUMBER_OF_CRAWLERS")
  15. NUMBER_OF_CRAWLERS = charlie.nextInt();
  16. else if (varName == "SEED_URL")
  17. SEED_URL = charlie.next();
  18. else if (varName == "DOMAIN")
  19. DOMAIN = charlie.next();
  20. else if (varName == "DELAY")
  21. delay = charlie.nextInt();
  22. }

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

  1. OUTPUT_FILE_PATH = filename2.txt
  2. Exception in thread "main" java.util.NoSuchElementException
  3. at java.base/java.util.Scanner.throwFor(Scanner.java:937)
  4. at java.base/java.util.Scanner.next(Scanner.java:1478)
  5. 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

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

To parse this text file I am using this

  1. String line = "";
  2. BufferedReader buffRead = new BufferedReader(new FileReader((CONFIG_FILE_PATH)));
  3. Scanner charlie = new Scanner(line);
  4. String varName;
  5. while ((line = buffRead.readLine()) != null){
  6. System.out.println(line);
  7. charlie.useDelimiter(" = ");
  8. varName = charlie.next();
  9. System.out.println(varName);
  10. if (varName == "OUTPUT_FILE_PATH")
  11. OUTPUT_FILE_PATH = charlie.next();
  12. else if (varName == "MAX_CRAWL_DEPTH")
  13. MAX_CRAWL_DEPTH = charlie.nextInt();
  14. else if (varName == "NUMBER_OF_CRAWLERS")
  15. NUMBER_OF_CRAWLERS = charlie.nextInt();
  16. else if (varName == "SEED_URL")
  17. SEED_URL = charlie.next();
  18. else if (varName == "DOMAIN")
  19. DOMAIN = charlie.next();
  20. else if (varName == "DELAY")
  21. delay = charlie.nextInt();
  22. }

I get this output when running the code

  1. OUTPUT_FILE_PATH = filename2.txt
  2. Exception in thread "main" java.util.NoSuchElementException
  3. at java.base/java.util.Scanner.throwFor(Scanner.java:937)
  4. at java.base/java.util.Scanner.next(Scanner.java:1478)
  5. 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是因为您在此行中实例化扫描器时提供的字符串:

  1. Scanner charlie = new Scanner(line);

实际上是:

  1. Scanner charlie = new Scanner("");

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

您应该执行以下操作:

  1. 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:

  1. Scanner charlie = new Scanner(line);

Is actually just:

  1. Scanner charlie = new Scanner("");

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

You should be doing the following:

  1. 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:

确定