9

In my program I want an integer input by the user. I want an error message to be show when user inputs a value which is not an integer. How can I do this. My program is to find area of circle. In which user will input the value of radius. But if user inputs a character I want a message to be shown saying Invalid Input.

This is my code:

int radius, area;
Scanner input=new Scanner(System.in);
System.out.println("Enter the radius:\t");
radius=input.nextInt();
area=3.14*radius*radius;
System.out.println("Area of circle:\t"+area);
Tome
  • 3,234
  • 3
  • 33
  • 36
Belal Khan
  • 2,099
  • 2
  • 22
  • 32

6 Answers6

28

If you are getting the user input with Scanner, you can do:

if(yourScanner.hasNextInt()) {
    yourNumber = yourScanner.nextInt();
}

If you are not, you'll have to convert it to int and catch a NumberFormatException:

try{
    yourNumber = Integer.parseInt(yourInput);
}catch (NumberFormatException ex) {
    //handle exception here
}
BackSlash
  • 21,927
  • 22
  • 96
  • 136
7

You can try this way

 String input = "";
 try {
   int x = Integer.parseInt(input); 
   // You can use this method to convert String to int, But if input 
   //is not an int  value then this will throws NumberFormatException. 
   System.out.println("Valid input");
 }catch(NumberFormatException e) {
   System.out.println("input is not an int value"); 
   // Here catch NumberFormatException
   // So input is not a int.
 } 
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
2

Using Integer.parseIn(String), you can parse string value into integer. Also you need to catch exception in case if input string is not a proper number.

int x = 0;

try {       
    x = Integer.parseInt("100"); // Parse string into number
} catch (NumberFormatException e) {
    e.printStackTrace();
}
Steve
  • 486
  • 2
  • 8
  • 19
Dark Knight
  • 8,218
  • 4
  • 39
  • 58
  • I am very beginner and cannot understand your code. Can you please explain it? – Belal Khan Nov 12 '13 at 09:13
  • Also, you may want to change area from int to double. As with int you'll loose precision value. – Dark Knight Nov 12 '13 at 09:22
  • 1
    Yeah that seems a bit complicated. What you can read with the Scanner is a String. What @DarkKnight does right here, it's trying to convert the string into an integer (the parseInt line). The try/catch semantics is just there to notice if the program crashes. It means that the program will try something and if it *crashes*, it will do something. – Fabinout Nov 12 '13 at 09:22
  • @Fabinout Thanks a lot for putting it clearly. – Dark Knight Nov 12 '13 at 09:23
1

If the user input is a String then you can try to parse it as an integer using parseInt method, which throws NumberFormatException when the input is not a valid number string:

try {

    int intValue = Integer.parseInt(stringUserInput));
}(NumberFormatException e) {
    System.out.println("Input is not a valid integer");
}
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
1

You can use try-catch block to check for integer value

for eg:

User inputs in form of string

try
{
   int num=Integer.parseInt("Some String Input");
}
catch(NumberFormatException e)
{
  //If number is not integer,you wil get exception and exception message will be printed
  System.out.println(e.getMessage());
}
Kamlesh Arya
  • 4,864
  • 3
  • 21
  • 28
1
        String input = "";
        int inputInteger = 0;
        BufferedReader br    = new BufferedReader(new InputStreamReader (System.in));

        System.out.println("Enter the radious: ");
        try {
            input = br.readLine();
            inputInteger = Integer.parseInt(input);
        } catch (NumberFormatException e) {
            System.out.println("Please Enter An Integer");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        float area = (float) (3.14*inputInteger*inputInteger);
        System.out.println("Area = "+area);
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55