title: Prevent user from inputting value larger than int max?
body: <p>I have some cool code that takes an int value. I let my little brother test it, and what is the first thing he does? He enters this:</p>
  
  <pre><code>12345678910
  </code></pre>
  
  <p>And he got this error:</p>
  
  <pre><code>User did not input a number. (Error Code 1)
  </code></pre>
  
  <p>Well that's not true. Is there a way to give him a different error for "value too large"? Here's my code:</p>
  
  <pre><code>try
  {
      number = input.nextInt();
  }
  catch (InputMismatchException e)
  {
      System.err.println("User did not input a number. (Error Code 1)");
      System.exit(1);
  }
  </code></pre>
  
  <p>Thanks!</p>
  
  <h1>EDIT</h1>
  
  <p>The code that was posted that I used has been modified. This is the code I ended up going with, but the solution is no longer in the comments.</p>
  
  <pre><code>        try 
          {
              double intitalinput = input.nextDouble();
  
              if (intitalinput &gt; Integer.MAX_VALUE)
              {
                  System.err.println("User entered a number larger than " + Integer.MAX_VALUE + ". (Error Code 2)");
                  System.exit(2);
              }
              else 
              {
                  number = (int) intitalinput;
              }
          }
          catch (InputMismatchException e) 
          {
              System.err.println("User did not input a number. (Error Code 1)");
              System.exit(1);
          }
  </code></pre>
  
  <p>Thank you to <a href="https://stackoverflow.com/users/2284613/jay-harris">Jay Harris</a> for solving my issue!</p>
  
  <h1>EDIT THE SECOND</h1>
  
  <p>I added a 'less than zero' check. In case anyone else stumbles upon this question wanting similar help, I'll show the updated code here:</p>
  
  <pre><code>try 
  {
      double intitalinput = input.nextDouble();
  
      if (intitalinput &gt; Integer.MAX_VALUE)
      {
          System.err.println("User entered a number larger than " + Integer.MAX_VALUE + ". (Error Code 2)");
          System.exit(2);
      }
      else if (intitalinput &lt; 0)
      {
          System.err.println("User entered a number smaller than 0. (Error Code 3)");
          System.exit(3);
      }
      else 
      {
          number = (int) intitalinput;
      }
  }
  catch (InputMismatchException e) 
  {
      System.err.println("User did not input a number. (Error Code 1)");
      System.exit(1);
  }
  </code></pre>