title: Finding end of file in Java
body: <p>I have a method that loops through a file to find the number of lines.</p>
  <pre><code>static int findFileSize(Scanner f){
          System.out.println(&quot;Got to counter:&quot;);
          String str;
          int line_count = 0;
          while(f.hasNextLine()) {
              line_count++;System.out.println(line_count);
              //str = f.nextLine();
          }System.out.println(&quot;File size: &quot; + line_count);
          return line_count;
      }//end findFileSize 
  </code></pre>
  <p>When i don't include the str = f.nextLine();, i can see it indexing indefinitely. What causes this to happen? And is there a way of finding the number of lines in a .txt file without needing to unnecessarily store a data into a string?</p>