title: How to use .nextInt() and hasNextInt() in a while loop
body: <p>So I want my program to read an input, which has some integers in one line, for example:</p>
  
  <p>1 1 2</p>
  
  <p>Then it should read every integer separately and print it in a new line. The number of integers the program has to read is not given in advance, so what I am trying to do is use a while loop, which ends after there are no more integers to read. This is the code I wrote:</p>
  
  <pre><code>while (scan.hasNextInt()) {
      int x = scan.nextInt();
      System.out.println(x);
  }
  </code></pre>
  
  <p>but it's not working correctly, because the loop never ends, it just wants the user to input more integers. What am I missing here?     </p>