简体   繁体   中英

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

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<Integer> arrayList = new ArrayList<>();

        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("Foreach print:");

            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:

在此处输入图像描述

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

Greetings from Vienna/Austria

Fabian

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);
}

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);
}

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);
}

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);
            

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);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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