title: Only allow integer input in Java
body: <p>I have the following code, which works well with integers, but throws the following exception when a string is entered:</p>
  <pre><code>Exception in thread &quot;main&quot; java.lang.NumberFormatException: For input string: &quot;henry&quot;
     at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
     at java.lang.Integer.parseInt(Integer.java:449)
     at java.lang.Integer.parseInt(Integer.java:499)
     at shipCost.java.shipCost.main(shipCost.java:13)
  </code></pre>
  <p>What would be the best approach to only allowing integers?</p>
  <pre><code>package shipCost.java;
  import java.io.*;
  
  public class shipCost {
  
      public static void main(String[] args) {
  
          String s1 = getInput(&quot;enter the item price&quot;);
          double input = Integer.parseInt(s1);
      
          if(input &lt; 100){ 
              double total = input += input * .02;
              System.out.println(total);
              return stdin.readLine();
          }else{
              double total = input;
              System.out.println(total);
          } 
  
      }
  
     private static String getInput(String prompt)
          {
          BufferedReader stdin = new BufferedReader(
                  new InputStreamReader(System.in));
          System.out.print(prompt);
          System.out.flush();
          try{
              return stdin.readLine();
          } catch (Exception e){
              return &quot;Error: &quot; + e.getMessage();
          }
      }
  
  
  }
  </code></pre>