Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: BufferedReader skips odd numbered lines

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default 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:

    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:
    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:
    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?
    Attached Files Attached Files
    • File Type: txt a.txt (149 Bytes, 1 views)


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: BufferedReader skips odd numbered lines

    Hello beer-in-box!

    It's pretty simple what is wrong with your 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.

  3. The Following User Says Thank You to andreas90 For This Useful Post:

    beer-in-box (February 14th, 2013)

  4. #3
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default 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
    Last edited by beer-in-box; February 14th, 2013 at 05:55 AM. Reason: Figured it out.

  5. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: BufferedReader skips odd numbered lines

    Quote Originally Posted by beer-in-box View Post
    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 View Post
    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:
    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:
    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.

  6. The Following User Says Thank You to andreas90 For This Useful Post:

    beer-in-box (February 14th, 2013)

  7. #5
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default 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

Similar Threads

  1. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 07:15 AM
  2. numbered months
    By JavaN00b101 in forum Java Theory & Questions
    Replies: 5
    Last Post: February 8th, 2012, 12:13 PM
  3. [SOLVED] New at Java... my if, else if, else program doesn't seem to work, skips to else.Help!
    By KevinE in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 1st, 2010, 03:51 PM
  4. Odd/Even
    By SOK in forum What's Wrong With My Code?
    Replies: 16
    Last Post: September 27th, 2010, 07:47 AM
  5. Java error while using BufferedReader class to read a .txt document
    By Jchang504 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: February 4th, 2009, 06:55 PM