title: How to check the type of a user input in Java?
body: <p>I was wondering how you can do type checking for a user input. Here I have a simple test to check if the user input is between 1 and 10. I want to set it up so that the user can also enter a letter, primarily so I can use the input 'q' to quit the program.</p>
  
  <p>Is there a part of the scanner that can type check? My thought was to have an if statement: if user inputs type int continue, if it is not type int, check if it is q to quit the program, else output this is an error. Below is what I have so far, it throws an expression when a number is put in since the types do not match.</p>
  
  <pre><code>public static void main(String[] args) {
      //Create new scanner named Input
      Scanner Input = new Scanner(System.in);
      //Initialize Number as 0 to reset while loop
      int Number = 0;
  
      do
      {
          //Ask user to input a number between 1 and 10
          System.out.println("At anytime please press 'q' to quit the program.");
          System.out.println();
          System.out.print("Please enter a number between 1 and 10:");
          Number = Input.nextInt();
  
          if (Number == 'Q'|| Number == 'q')
          {
              System.out.println("You are now exiting the program");
          }
          else if (Number &gt;= 1 &amp;&amp; Number &lt;= 10)
          {
              System.out.println("Your number is between 1 and 10");
          }   
          else
          {
              System.out.println("Error: The number you have entered is not between"
                      + " 1 and 10, try again");
          }
      }
      //Continue the loop while Number is not equal to Q
     while (Number != 'Q' &amp; Number != 'q');
  }
  
  }
  </code></pre>
  
  <p>Thanks everyone for the responses. I am a bit new so the try statement is new to me but looks like it will work (seems somewhat self explanatory of what it does). I will look into its use more and implementing it correctly.</p>