BufferReader question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shana07
    Contributor
    • Jan 2007
    • 280

    BufferReader question

    Hi all...I just wanna confirm about my syntax. please feel free to give any comments. I have these codes:
    Code:
    BufferedReader in1 = new BufferedReader (new FileReader( "A.txt")); 
    BufferedReader in2 = new BufferedReader (new FileReader( "B.txt")); 
                      
    lineFile1 = in1.readLine(); 
    lineFile2 = in2.readLine();
              
    while((lineFile2 != null) [B]&&[/B] (lineFile1 != null))
    { ...
    lineFile1 = in1.readLine(); 
    lineFile2 = in2.readLine();
    The while sytax above shows that my program will read file A & B from first until end lines.My Qs:
    1. I just don't get what if I put
    Code:
    while((lineFile2 != null) [B]&&[/B] (lineFile1 != null))
    because when I run my program, it shows no different..but I'm not sure...

    2. How to make sure comparison between 2 files that having not equal total line? I mean, need to perform the comparison until finish lines eventhough one of the file might have less line.

    Thank you very much
  • prometheuzz
    Recognized Expert New Member
    • Apr 2007
    • 197

    #2
    Originally posted by shana07
    Hi all...I just wanna confirm about my syntax. please feel free to give any comments. I have these codes:
    Code:
    BufferedReader in1 = new BufferedReader (new FileReader( "A.txt")); 
    BufferedReader in2 = new BufferedReader (new FileReader( "B.txt")); 
                      
    lineFile1 = in1.readLine(); 
    lineFile2 = in2.readLine();
              
    while((lineFile2 != null) [B]&&[/B] (lineFile1 != null))
    { ...
    lineFile1 = in1.readLine(); 
    lineFile2 = in2.readLine();
    The while sytax above shows that my program will read file A & B from first until end lines.
    It reads one line at a time from both buffers until one of the buffers is at it's end, then it stops (although one buffer might still have more lines in it).


    Originally posted by shana07
    ...
    2. How to make sure comparison between 2 files that having not equal total line? I mean, need to perform the comparison until finish lines eventhough one of the file might have less line.

    Thank you very much
    Try something like this:
    Code:
    lineFile1 = in1.readLine(); 
    lineFile2 = in2.readLine();
              
    while(!(lineFile1 == null && lineFile2 == null))
    {
      if(lineFile1 != null) lineFile1 = in1.readLine(); 
      if(lineFile2 != null) lineFile2 = in2.readLine();
      // ...
    }
    It will keep looping until both lineFile1 and lineFile2 are null.

    Comment

    • shana07
      Contributor
      • Jan 2007
      • 280

      #3
      Originally posted by prometheuzz
      It reads one line at a time from both buffers until one of the buffers is at it's end, then it stops (although one buffer might still have more lines in it).




      Try something like this:
      Code:
      lineFile1 = in1.readLine(); 
      lineFile2 = in2.readLine();
                
      while(!(lineFile1 == null && lineFile2 == null))  //I put your codes - to read until finish lines for both files.
      {
        if(lineFile1 != null) lineFile1 = in1.readLine(); 
        if(lineFile2 != null) lineFile2 = in2.readLine();
        // ...
      }
      It will keep looping until both lineFile1 and lineFile2 are null.
      Sorry, my first question above should be (the second while)
      Code:
      while((lineSEC != null) [B]||[/B] (lineFRST != null))
      1. If I put that - what it is going to be then....

      Anyway, I got your point...thanks a lot..

      2. One more question, about comparing two buffers. e.g.
      buffer1: buffer2:
      23 Diana 23 Anne
      25 Peter 30 Alen
      40 Christ 45 Shannon
      60 Alex

      am I right by having these codes to compare them:
      Code:
      .....
      lineFile1 = in1.readLine(); 
      lineFile2 = in2.readLine();
                
      while(!(lineFile1 == null && lineFile2 == null))
      {
        if(lineFile1 != null) lineFile1 = in1.readLine(); 
        if(lineFile2 != null) lineFile2 = in2.readLine();
        // ...
                  String[] thelineFRST = lineFile1.split(" ");
                  String add1 = thelineFRST[0];
                  String name1 = thelineFRST[1];
                              
                  String[] thelineSEC = lineFile2.split(" ");
                  String add2 = thelineSEC[0];
                  String name2 = thelineSEC[1];
                     
                  if(add2 .equals(add1))  // if their addresses are same 
                  {
                     if(!(name2.equals(name1))) // and their names are different
                           { 
                             printWriter.println(add2) ; //get the add                                
                           }
                          else 
                              {
                                printWriter.println(" "); // not interested
                              }      
                   }  
                  lineFile1 = in1.readLine(); 
                  lineFile2 = in2.readLine();
                  printWriter.close();  
                 }
      Please advise me...thanks

      Comment

      Working...