1 Attachment(s)
BufferedReader skips odd numbered lines
I was studying on a project where I'd like to read a text file and put the content into the program.
For this, I needed to read it, obviously and I tried. But it only read second and fourth and so lines. So, I thought it would be better to work on a different project to figure the problem.
So, here is my code:
Code :
import java.io.*;
public class SolveHere {
private File f;
private BufferedReader r;
private FileReader fr;
public static void main(String[] args) {
SolveHere m = new SolveHere();
m.fileRead();
}
public void fileRead(){
f = new File("D:\\a.txt");
try {
fr = new FileReader(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
r = new BufferedReader(fr);
String w;
try {
while((r.readLine())!=null){
w = r.readLine();
System.out.println(w);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Attached you can find the text file I'm working on for this test project. Since my OS language is not English I thought the problem might be it.
This is what I expect:
Quote:
This is first line.
This is second line.
This is third line.
This is fourth line.
This is fifth line.
This is sixth line.
This is seventh file.
This is what I get:
Quote:
This is second line.
This is fourth line.
This is sixth line.
null
I am so annoyed right now as you'd expect :) Can you guys explain me what is wrong with this code?
Re: BufferedReader skips odd numbered lines
Hello beer-in-box!
It's pretty simple what is wrong with your code.
Code :
try {
while((r.readLine())!=null){
w = r.readLine();
System.out.println(w);
}
In this snippet of code you make two calls to the readLine() method, one in the while condition and one inside the loop, but you only print the second one. If you make a little search on how to use the BufferedReader class to read text files, you will find the right way to do it. Actually you only have to call readLine() once in each iteration and assign it the String variable you have.
Hope this makes sense.
Re: BufferedReader skips odd numbered lines
Hi,
I just came back to say I figured it out and here is your answer :) Thank you.
But, can you explain the steps the compiler takes to produce such results in my case? To be honest, although I know what is correct, I still cannot see what is wrong with my previous code.
EDIT: Oh, it reads the first line in the while(), and when I call it second time, naturally it reads the next line. Thus, first line is forgotten.
I am not calling a variable when I type w = r.readLine(), I call the method. And what method does is to read the next line :)
This question would not exist if I could be a little more patient :D
Re: BufferedReader skips odd numbered lines
Quote:
Originally Posted by
beer-in-box
Hi,
I just came back to say I figured it out and here is your answer :) Thank you.
You are welcome.
Quote:
Originally Posted by
beer-in-box
But, can you explain the steps the compiler takes to produce such results in my case? To be honest, although I know what is correct, I still cannot see what is wrong with my previous code.
Each time you call the readLine() method a line in your text file is read.
Your text file is:
Quote:
This is first line.
This is second line.
This is third line.
This is fourth line.
This is fifth line.
This is sixth line.
This is seventh file.
and your code:
Code :
try {
while((r.readLine())!=null){
w = r.readLine();
System.out.println(w);
}
First iteration:
In the while loop condition r.readLine() is called--> ie This is first line. (the condition is true, but you don't save it in a variable so it is lost)
Inside the loop w = r.readLine(); --> ie This is second line. (you save it in the w variable and you print it.)
Second iteration:
In the while loop condition r.readLine() is called --> ie This is third line. (the condition is true, but you don't save it in a variable so it is lost)
Inside the loop w = r.readLine(); --> ie This is fourth line. (you save it in the w variable and you print it.)
Third iteration:
In the while loop condition r.readLine() is called --> ie This is fifth line. (the condition is true, but you don't save it in a variable so it is lost)
Inside the loop w = r.readLine(); --> ie This is sixth line. (you save it in the w variable and you print it.)
Fourth iteration:
In the while loop condition r.readLine() is called --> ie This is seventh file. (the condition is true, but you don't save it in a variable so it is lost)
Inside the loop w = r.readLine(); --> ie null (there isn't a line in your text file , null is returned and you save it in the w variable and print it.)
Fifth iteration:
In the while loop condition r.readLine() is called --> ie null (the condition is false)
And a side note: this happens during runtime. The compiler just checks for syntax errors at compilation time.
Re: BufferedReader skips odd numbered lines
I guess you didn't see my edit :(
I am sorry for making trouble of explaining these to you.
However, this is a better explanation and, I appreciate it :)