title: Infinite loop on Scanner.hasNext, reading from a file
body: <p>I'm apparently facing an infinite loop on <code>while(input.hasNext())</code> , as in following code</p>
  
  <pre><code>File file = new File("data.txt");
  Scanner input = new Scanner(file);
  
  int sum = 0;
  
  while (input.hasNext()) {
      if (input.hasNextInt()) {
          sum += input.nextInt();
      }
  }
  System.out.println(sum);
  </code></pre>
  
  <p>Some related questions explain why it always returns true if input stream is <code>System.in</code>, however I'm scanning through a <code>File</code>.</p>
  
  <p>Please let me know where I'm going wrong.</p>
  
  <p>I'm attempting to calculate the sum of unique integer values (space delimited where they occur).</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Lesson learned, 
   <code>input.hasNext()</code> does not move the pointer to the next line and thus the scanner does not advance past any input. As explained in answers to this question as well as <a href="https://stackoverflow.com/questions/8352040/scanner-on-text-file-hasnext-is-infinite">here</a> .</p>