读取包含每行数字的txt文件时的输出。

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

Output when I read in a txt.file with numbers in each line

问题

Sure, here is the translation of the provided text:

我想要从以下文本文件中读取数字:

5
8
-2
11
3
-5
2
10


这是我的 Java 代码:

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
InputStream inStream = null;
ArrayList arrayList = new ArrayList<>();

    try {
        String userInput = bufferedReader.readLine();
        inStream = new FileInputStream(userInput);
        BufferedReader readerFile = new BufferedReader(new InputStreamReader(inStream));

        while(readerFile.ready()) {
            int number = Integer.parseInt(readerFile.readLine());
            arrayList.add(number);
        }
        System.out.println("Foreach print:");

        for(int number:arrayList) {
            System.out.println(number);
        }
    }

    catch (Exception e) {
        System.out.print(e.getClass().getSimpleName());
    }

    finally {
        bufferedReader.close();
        inStream.close();
    }

}

}


像这样,代码完美运行。我不理解的是,当我将行```System.out.println(Integer.parseInt(readerFile.readLine()));``` 作为代码的一部分,以便查看它读取了什么时。其余部分保持不变。我得到了这个结果:

[![enter image description here][1]][1]

为什么会得到这个结果?我完全困惑了。提前感谢您的帮助!

来自奥地利维也纳的问候

法比安
英文:

I want to read the numbers from the following text file:

5
8
-2
11
3
-5
2
10

This is my java code:

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        InputStream inStream = null;
        ArrayList&lt;Integer&gt; arrayList = new ArrayList&lt;&gt;();

        try {
            String userInput = bufferedReader.readLine();
            inStream = new FileInputStream(userInput);
            BufferedReader readerFile = new BufferedReader(new InputStreamReader(inStream));

            while(readerFile.ready()) {
//                System.out.println(Integer.parseInt(readerFile.readLine()));
                int number = Integer.parseInt(readerFile.readLine());
                arrayList.add(number);
            }
            System.out.println(&quot;Foreach print:&quot;);

            for(int number:arrayList) {
                System.out.println(number);
            }
        }

        catch (Exception e) {
            System.out.print(e.getClass().getSimpleName());
        }

        finally {
            bufferedReader.close();
            inStream.close();
        }

    }
}

Like this the code works perfectly.
What I don`t understand is when I use the line System.out.println(Integer.parseInt(readerFile.readLine())); as part of my code in order to see what he reads. The rest remains unchanged. I get this result:

读取包含每行数字的txt文件时的输出。

Why do I get this result? I`m totally confused. Thx in advance!

Greetings from Vienna/Austria

Fabian

答案1

得分: 2

我理解的是您两次调用了readLine,一次带有打印,一次没有打印。

要避免这种情况,请使用以下代码:

while(readerFile.ready()) {
   int number = Integer.parseInt(readerFile.readLine());
   System.out.println(number);
   arrayList.add(number);
}
英文:

what I understand is that you are calling readLine twice.. one with and one without printing.

to avoid this, use the following code:

while(readerFile.ready()) {
   int number = Integer.parseInt(readerFile.readLine());
   System.out.println(number);
   arrayList.add(number);
}

答案2

得分: 2

每次调用readerFile.readLine()时,它会读取一行。所以当你取消注释你已经注释的那一行时,一半的行被该代码吃掉,而另一半被它之后的行吃掉。

while(readerFile.ready()) {
    String line = readerFile.readLine();
    System.out.println(Integer.parseInt(line));
    int number = Integer.parseInt(line);
    arrayList.add(number);
}

可以修复它。或者更好的方法是:

while(readerFile.ready()) {
    int number = Integer.parseInt(readerFile.readLine());
    System.out.println(number);
    arrayList.add(number);
}
英文:

Every time you call readerFile.readLine() it reads one line. So when you uncomment the line you have commented, then half the lines are being eaten by that code and half are being eaten by the line after it.

while(readerFile.ready()) {
    String line = readerFile.readLine();
    System.out.println(Integer.parseInt(line));
    int number = Integer.parseInt(line);
    arrayList.add(number);
}

would fix it. Or better still:

while(readerFile.ready()) {
   int number = Integer.parseInt(readerFile.readLine());
   System.out.println(number);
   arrayList.add(number);
}

答案3

得分: 1

输出发生是因为,当您取消注释System.out.println(Integer.parseInt(readerFile.readLine()))行时,在while循环内您每次读取两行

建议的做法是,将数字读入一个变量,然后将这个变量添加到列表并打印出来:

while (readerFile.ready()) {
    int number = Integer.parseInt(readerFile.readLine());
    arrayList.add(number);
    System.out.println(number);
}
英文:

That output occurs because, when you uncomment the System.out.println(Integer.parseInt(readerFile.readLine())) line, you are reading two lines at a time within the while loop.

As suggested, read the number to a variable and then add this variable to the list and print it:

while(readerFile.ready()) {
    int number = Integer.parseInt(readerFile.readLine());
    arrayList.add(number);
    System.out.println(number);
}

答案4

得分: 1

问题出在readerFile.readLine(),因为每次调用**readerFile.readLine()**时,它会指向下一行。你每次调用它时只是利用一个引用。为了修复上面的代码,请按以下方式更改代码。

int number = Integer.parseInt(readerFile.readLine());    
System.out.println(number);
英文:

The problem is with the readerFile.readLine(), because each time when you call readerFile.readLine(), it will point to next line. You are just utilizing a reference when you call it eachtime. for fixing the above code. Change the code as follows.

int number = Integer.parseInt(readerFile.readLine());    
System.out.println(number);

答案5

得分: 0

readerFile.readLine()

  1. 返回字符串 - 当前行指向的内容
  2. 然后指向下一行
    (与迭代器接口相同工作方式)

这意味着调用.readLine()两次将使您跳过行 - 1、3、5 ......,并且您将只获取行2、4、6 ......(如果我们从1开始计数)

我建议使用以下代码:

String sCurrentLine;

while ((sCurrentLine = readerFile.readLine()) != null) {
    int number = Integer.parseInt(sCurrentLine);
    System.out.println(number);
    arrayList.add(number);
}
英文:

readerFile.readLine()

  1. return String - the currenct line is pointing at
  2. and then pointing to then next line
    (working same has iterator interface)

which means that calling .readLine() twice will make you skip the line - 1, 3, 5 .....and you will get only the lines 2, 4, 6 ....
(if we start counting from 1)

i would recomnd this:

        String sCurrentLine;

       while ((sCurrentLine = readerFile.readLine()) != null) {
               int number = Integer.parseInt(sCurrentLine);
               System.out.println(number);
               arrayList.add(number);
            }

huangapple
  • 本文由 发表于 2020年7月22日 20:39:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63034414.html
匿名

发表评论

匿名网友

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

确定