title: Cannot use Scanner to read only one line at a time in Java
body: <p>I am trying to read a file line by line in java. Here is my code:</p>
  
  <pre><code>Scanner s= new Scanner(new FileReader("outputfile.txt"));
      String line = null;
      while (!(line = s.nextLine()).contains("OK")) {
          if (line.contains("BOOK")) {
              //do something
          }   
      }
  </code></pre>
  
  <p>What i am trying to do is, i read the file line by line, and if the next line has "OK" in it, then i stop reading. But the problem is, since i have</p>
  
  <pre><code>!(line = s.nextLine()).contains("OK")
  </code></pre>
  
  <p>every time i get into the line </p>
  
  <pre><code>if (line.contains("BOOK")), 
  </code></pre>
  
  <p>since line=s.nextLine() 
  i read another line and in one loop cycle i read two lines. How can i fix this?</p>
  
  <p>Thanks</p>