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: why does this code read an html file uncontinuously

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default why does this code read an html file uncontinuously

    I am using this code to read a file, when I do it with a for loop it works properly but when I use while loop it reads the first line, leaves the second line, reads the third line, and...
    try {
            		   while(mybuffer.readLine() != null){	
            			   	data.add(mybuffer.readLine());
    						System.out.println(data.elementAt(i));
    						i++;
    					}
    	           }     
     
            		catch (IOException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();}
    				}   
     
                }
    Last edited by helloworld922; March 25th, 2010 at 08:48 PM.


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

    Default Re: why does this code read an html file uncontinuously

    Based on the assumption myBuff is a Reader (aka BufferedReader), every time you call readLine it reads the line and moves the pointer to the next, and as is the while loop skips over every other line because you do not keep the readLine data in memory...one workaround:
    String line = null;
    while ( ( line = myBuff.readLine() ) != null ){//store the readLine data in memory
    //use the data in memory to add to your data object
    }

  3. #3
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: why does this code read an html file uncontinuously

    but why it works with for loop without saving in memory. the code below works properly.
    try {
    for(int j =0; j<250; j++){
    data.add(mybuffer.readLine());
    System.out.println(data.elementAt(i));
    i++;
    }
    }

    catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();}
    }

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

    Default Re: why does this code read an html file uncontinuously

    Quote Originally Posted by nasi View Post
    but why it works with for loop without saving in memory. the code below works properly.
    Look closely at your original while code - every call to readLine moves the pointer in the file forward to the next line
    while ( myBuffer.readLine() != null ){///reads line and moves the pointer forward (lines 1, 3, 5...)
        data.add(mybuffer.readLine());//reads and stores the line (lines 2, 4, 6...)
    }

    Your for loop only calls readLine() a single time per loop, as opposed to the original while loop where it is called twice per loop

  5. #5
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: why does this code read an html file uncontinuously

    ohhhhhhhhh, I got it. thank you very much.

    Best wishes

Similar Threads

  1. Implementing HTML tags in Java Source Code
    By bookface in forum Java Theory & Questions
    Replies: 4
    Last Post: March 19th, 2010, 09:29 PM
  2. New person Just trying to read a file of ints
    By dubois.ford in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 7th, 2010, 11:47 PM
  3. Read data from text file
    By yroll in forum Algorithms & Recursion
    Replies: 4
    Last Post: December 31st, 2009, 01:40 AM
  4. 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
  5. How to Read a Portion of a File in Java?
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 7th, 2009, 04:16 PM

Tags for this Thread