Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

IO buffer is not picking up all .txt file data

Code snippet:
// create a BufferedReader for the file
BufferedReader bfr = new BufferedReader(new FileReader(f));
String text;

while (( text = bfr.readLine()) != null ){
int intLine = Integer.parseInt(bfr.readLine());
System.out.println( intLine + "\n");
}
The .txt file it is reading contains 11 lines, each with a number but the output is picking up only the even numbered lines. IE: file of (10 3 50 40 100 17 531 749 85 1246 68) will only output: 3-40-17-749-1246.
At the end, I'm also throwing a non-integer error but if I had to guess I would say that is because there is a carriage return after the last number?
[693 byte] By [JavaBeanie] at [2007-11-11 6:49:08]
# 1 Re: IO buffer is not picking up all .txt file data
You are reading the line twice

while (( text = bfr.readLine()) != null ){ // <-- here
int intLine = Integer.parseInt(bfr.readLine()); // <-- and here
System.out.println( intLine + "\n");
}

BufferedReader.readLine () gets a line, it doesn't include the line cr/lf.

Use this to ensure proper parsing:

while (( text = bfr.readLine()) != null ){
int intLine = Integer.parseInt(text.trim()); <-- trim is important
System.out.println( intLine + "\n");
}
sjalle at 2007-11-11 22:40:10 >
# 2 Re: IO buffer is not picking up all .txt file data
Thank you for pointing out my mistake. That fixed my problem.
JavaBeanie at 2007-11-11 22:41:16 >