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 10 of 10

Thread: going to the nextLine in Java BufferedReader

  1. #1
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Angry going to the nextLine in Java BufferedReader

    Hello guys,

    I have a text and I am reading each line in the text with the simple while loop:

    BufferedReader br = new BufferedReader(new FileReader(new File(a.txt)));
    string line = new String();
    while((line = br.readLine())!=null){
     if(line.equals("john"))
     //skip to next line
    else{
     //continue something else.. 
    }
    }

    My question is how do I skip to the next line ? Using apache.commons.io.FileUtils; one could easily have done something like this:

    LineIterator it = FileUtils.lineIterator(file, "UTF-8");
    String line = it.nextLine(); //this goes to the next line..

    How can this be done using BufferedReader ?

    Thanks


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    623
    Thanks
    0
    Thanked 112 Times in 97 Posts

    Default Re: going to the nextLine in Java BufferedReader

    The buffered reader should go to the next line when you call readLine() on it.
    How do you test to see that it isnt happening?
    Last edited by Cornix; July 18th, 2014 at 10:53 AM.

  3. #3
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: going to the nextLine in Java BufferedReader

    but, I dont see it having a method nextLine() or am I doing something wrong?

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    623
    Thanks
    0
    Thanked 112 Times in 97 Posts

    Default Re: going to the nextLine in Java BufferedReader

    I meant readLine, I am sorry.

  5. #5
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: going to the nextLine in Java BufferedReader

    readLine is to read the lines not skip to the next line.

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    623
    Thanks
    0
    Thanked 112 Times in 97 Posts

    Default Re: going to the nextLine in Java BufferedReader

    If you read the line it is removed from the stream. If you read it without using the information you will naturally skip it.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,310
    My Mood
    Mellow
    Thanks
    194
    Thanked 669 Times in 656 Posts

    Default Re: going to the nextLine in Java BufferedReader

    The while() loop you've posted will read each line in the file looking for "john", moving to the next line with each pass through the loop. Your question doesn't really make sense, UNLESS, you want to find the line "john" and then SKIP THE NEXT LINE, in which case, you'd write the if() statement:
    if(line.equals("john"))
        //skip to next line - read next line and discard it
        br.readLine();

  8. #8
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: going to the nextLine in Java BufferedReader

    Hi GregBrannon,

    I got a text file with the first line labels of the column ..e.g
    age name
    20 john
    13 Mike
    24 Tom

    So, what I am trying to do is read this first line and do something with it before continuing to the rows with their real age and name.

    I tried building a string .. and then comparing the string as in :
    if(line.contentEquals(stringBuilder){
    //do something with that line
    }

    //go to the next line..
    //and continue reading the lines e.g 20 john, 13 Mike e.t.c

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,310
    My Mood
    Mellow
    Thanks
    194
    Thanked 669 Times in 656 Posts

    Default Re: going to the nextLine in Java BufferedReader

    So the first line is a special case. In your example, I would say the first line defines the field names. If you know that (and you do) process the first line by recording the field names, and then process the rest of the lines as field values.

  10. The Following User Says Thank You to GregBrannon For This Useful Post:

    help_desk (July 18th, 2014)

  11. #10
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: going to the nextLine in Java BufferedReader

    Hi GregBrannon,

    I think I have to rephrase my question to get the right help.

    I want to do something with the first line in the text and only and only after that, can I start looping through the rest of the lines in the text.

    so, I tried this:

    line = br.readLine() //this is the first line..

    //I do something with that line..

    //now I want to skip to the next line..but don't know how to do it because when I run the next lines of code
    //the nextLine is to read from the second line of the text NOT the first line anymore.. it is how to set the br to the nextLine, that I am trying to figure out..
    while((nextLine =br.readLine())!=null){
    //continue what I was doing..
    }

    Like I said, this can easily be done using apache.commons.io.FileUtils; as below:

    LineIterator it = FileUtils.lineIterator(file, "UTF-8");
    String line = it.nextLine(); //this goes to the next line..

    but how to do the exact same thing with BufferedReader is my main question ?

    --- Update ---

    OK, I figured it out now. It seems, using BufferedReader doesn't have any method like nextLine(). one just has to do do what one has to do with the line and move to the while loop. This is for those who might come across this similar problem in the future.

Similar Threads

  1. scanner.nextLine()
    By R0bsterz in forum Java Theory & Questions
    Replies: 13
    Last Post: April 25th, 2013, 06:26 AM
  2. I need help! Re: strings, nextLine, while and more..
    By rockerade in forum What's Wrong With My Code?
    Replies: 19
    Last Post: October 5th, 2010, 03:51 PM
  3. Replies: 1
    Last Post: August 13th, 2010, 06:58 AM
  4. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM

Tags for this Thread