简体   繁体   English

Java 的 BufferedReader.readLine() 行为异常

[英]Java's BufferedReader.readLine() behaving unexpectedly

I have a file that contains scores for some hymns.我有一个文件,其中包含一些赞美诗的乐谱。 It is supposed to be read by a Java program that will store these hymns as objects.它应该由 Java 程序读取,该程序会将这些赞美诗存储为对象。 Each hymn is written like this:每首赞美诗都是这样写的:

1
Hallelujah
C
Rythm

      C                  Am
I've |heard there was a |secret chord
$

The '$' is just a token for EOF. '$' 只是 EOF 的标记。 I'm using BufferedReader.readLine() to read line by line until the 'Rhytm' line to initialize a Hymn object:我正在使用BufferedReader.readLine()逐行读取,直到“Rhytm”行初始化 Hymn object:

reader = new BufferedReader(new FileReader("/home/hal/teste.txt"));     
String linha = "", titulo, r, t; int n;
while(linha!="$") {
    n = Integer.parseInt(reader.readLine());     //reading the number
    titulo = reader.readLine();                  //reading the title
    Tom tom = new Tom(reader.readLine());        //reading the tone
    Ritmo ritmo = new Ritmo(reader.readLine());  //reading the rythm

    Hino hino = new Hino(n, titulo, tom, ritmo); //initializing the object

And then I just read each line separatedly.然后我只是分别阅读每一行。 The problem is, the first readLine() executes, it reads the sixth line (" C Am "), not the first one(" 1 ").问题是,第一个readLine()执行时,它读取的是第六行(“ C Am ”),而不是第一行(“ 1 ”)。 I've tried using Scanner.nextInt() to get that number, but it gives me the same error.我尝试使用Scanner.nextInt()来获取该数字,但它给了我同样的错误。 That's my console's output:那是我的控制台的 output:

Exception in thread "main" java.lang.NumberFormatException: For input string: "      C                  Am"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.base/java.lang.Integer.parseInt(Integer.java:638)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)
    at project/project.Build.main(Build.java:22)

Build.java:22 is where n = Integer.parseInt(reader.readLine()); Build.java:22是其中n = Integer.parseInt(reader.readLine()); is located.位于。 What am I doing wrong?我究竟做错了什么?

Only the first line of your file contains an integer, so you should not parse it within the while loop.只有文件的第一行包含 integer,因此不应在 while 循环中解析它。

Within the loop you should process only the lines that are repeatable.在循环中,您应该只处理可重复的行。 As @Fureeish mentioned you also need to compare string using equals.正如@Fureeish 提到的,您还需要使用等号比较字符串。

Usually parsing lines looks something like this:通常解析行看起来像这样:

for (String linha = reader.readLine(); linha != null && !linha.equals("$"); linha = reader.readLine()) {
       ...
}

With this for loop you iterate through the file line by line.使用这个 for 循环,您可以逐行遍历文件。 Within the loop you should have handling for the different possibilities.在循环中,您应该处理不同的可能性。

The best way would probably be to handle the first 4 lines outside the loop and then have the three distinctive cases within:最好的方法可能是在循环外处理前 4 行,然后在其中包含三个不同的情况:

  1. it is an empty line这是一个空行
  2. The line consists of notes该行由注释组成
  3. The line represents the hymn text该行代表赞美诗文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM