[java] How to read until end of file (EOF) using BufferedReader in Java?

I have problem with reading the input until EOF in Java. In here, there are single input and the output consider the input each line.

Example:

input:

1
2
3
4
5

output:

0 
1
0
1
0

But, I have coded using Java, the single output will printed when I was entering two numbers. I want single input and print single output each line (terminate EOF) using BufferedReader in Java.

This is my code:

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
StringBuffer pr = new StringBuffer("");

String str = "";
while((str=input.readLine())!=null && str.length()!=0) {
    BigInteger n = new BigInteger(input.readLine());
}

This question is related to java io bufferedreader

The answer is


With text files, maybe the EOF is -1 when using BufferReader.read(), char by char. I made a test with BufferReader.readLine()!=null and it worked properly.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to io

Reading file using relative path in python project How to write to a CSV line by line? Getting "java.nio.file.AccessDeniedException" when trying to write to a folder Exception: Unexpected end of ZLIB input stream How to get File Created Date and Modified Date Printing Mongo query output to a file while in the mongo shell Load data from txt with pandas Writing File to Temp Folder How to get resources directory path programmatically ValueError : I/O operation on closed file

Examples related to bufferedreader

Java using scanner enter key pressed How to read until end of file (EOF) using BufferedReader in Java? How to use BufferedReader in Java Using BufferedReader.readLine() in a while loop properly Convert InputStream to BufferedReader Android Reading from an Input stream efficiently Scanner vs. BufferedReader Do I need to close() both FileReader and BufferedReader?