title: While Loop not integer
body: <p>I've created this code to get unlimited values of integers, store them, and calculate the mean. It also does it in a squared format. My problem is, I created it so that the while loop only stops when the the number 0 is entered. However, I only put it there as a substitute, as it's the only way i could test the rest of the code to ensure it all works.</p>
  
  <p>What I really want to do it have the while loop continue until the user provides input that is not an integer. Ive tried everything, using <code>hasNextint()</code> or <code>nextint</code> and so forth, what can I do?</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  
  public class InputStats {
    public static void main(String[] args) {
  
      Scanner TextIO = new Scanner(System.in);
      int inputNumber;     // One of the integers input by the user.
      int sum;             // The sum of all the integers.
      int count;           // The number of integers that have been entered.
      double mean;         // The mean value of the integers, rounded to 2 decimal places.
      int squarein;        // Value of squared number.
      int sumsquare;       // The sum of the squares of all the integers.
      double meansquare;   // The mean value of the squares of integers, rounded to 2 decimal places.
  
      /* Initialize the summation and counting variables. */
      sum = 0;
      count = 0;
      sumsquare = 0;
      meansquare = 0;
  
      /* Read and process the user's input. */
      System.out.println("Enter your first positive integer: ");
      inputNumber = TextIO.nextInt();
  
      while (inputNumber != 0) {
        sum += inputNumber;   // Add inputNumber to running sum.
        squarein =  inputNumber; //
        sumsquare = squarein * squarein; //square inputs
        count++;              // Count the input by adding 1 to count.
        System.out.println("Enter your next positive integer, or 0 to end: ");
        inputNumber = TextIO.nextInt();
      }
  
      /* Display the result. */
      if (count == 0) {
        System.out.println("You didn't enter any data!");
      } else {
        mean = ((double)sum) / count;
        meansquare = ((double)sumsquare) / count;
  
        TextIO.nextInt();
  
        System.out.println("Numbers entered: " + count + "\n");
        System.out.println("Sum: " + sum + "\n");
        System.out.println("Mean: " + mean + "\n");
        System.out.println("Sum Squares: " + sumsquare + "\n");
        System.out.println("Mean Square: " + meansquare + "\n");
      }
    } // end main()
  } // end class InputStats
  </code></pre>