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

Thread: BufferedReader starting to read on a line 3/4 of the way through the file, HELP!

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default BufferedReader starting to read on a line 3/4 of the way through the file, HELP!

    The code below is the first step I have taken to create a program for an assignment that
    is supposed to read from a file that contains information about gold medal winners from the Olympics.

    I have attached the .txt file I am using as my source for input.

    My problem is that for some reason, my BufferedReader starts reading a line 3/4 of the way through the file
    which is not what I need, I need it to start reading from the first line!

    Any help is greatly appreciated, my code is below!
    Thanks!

    import java.io.*;
     
    public class TestGoldMedals{
    	public static void main(String[] args){
    		BufferedReader input;
    		String line;
     
    		try{
    			input = new BufferedReader(new FileReader("a2a.txt"));
    			line = input.readLine();
     
    			while(line != null){
     
    				line = input.readLine();
    				System.out.println(line);
    			}
    			input.close();
    		}catch (IOException ioe){
    			System.out.println(ioe.getMessage());
    		}
    	}
    }
    Attached Files Attached Files
    Last edited by GeeRam; October 21st, 2011 at 03:50 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: BufferedReader starting to read on a line 3/4 of the way through the file, HELP!

    Write out on paper what is happening...a line is read, then another line is read, then the line variable printed. You want to read a line, print a line, read a line print a line, etc...

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: BufferedReader starting to read on a line 3/4 of the way through the file, HELP!

    Quote Originally Posted by copeg View Post
    Write out on paper what is happening...a line is read, then another line is read, then the line variable printed. You want to read a line, print a line, read a line print a line, etc...
    Yes so essentially, get rid of that first readLine() call after you instantiate the reader.

    I don't see though how it could be starting 3/4 of the way into the document? Are you sure?

Similar Threads

  1. Java program to read last line of a file
    By JavaPF in forum File Input/Output Tutorials
    Replies: 2
    Last Post: September 10th, 2009, 02:26 AM
  2. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  3. Reading a file line by line using the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 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