3

How would I use InputMismatchException to determine if the value entered into the Scanner is not an integer? Basically, if they enter in a word instead of an integer I need to use InputMismatchException to return a message.

while (true) {

        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        try{
            Integer.parseInt(i);
        } catch (InputMismatchException e) {
            System.out.println("Sorry, " + i + " is not a number.");
        }

        if (i == 1) {
            System.out.println("1 was selected");
        } else {
            System.out.println("1 was not selected");

        }
3
  • 2
    the error would be thrown by the line sc.nextInt(); not by doing Integer.parseInt(i); so put int i = sc.nextInt(); inside of a try catch with that Exception and it should work Commented Nov 10, 2015 at 4:01
  • can you please post what you mean? Commented Nov 10, 2015 at 4:04
  • i have posted below. Commented Nov 10, 2015 at 4:07

3 Answers 3

2

Change your code as such:

while (true) {
    Scanner sc = new Scanner(System.in);
    String s = sc.nextLine();
    try{
       int i = Integer.parseInt(s);

       if (i == 1) {
           System.out.println("1 was selected");
       } else {
           System.out.println("1 was not selected");
       }
    } catch (NumberFormatException e) {
        System.out.println("Sorry, " + s + " is not a number.");
    }


}

Changes:

  • Note use of nextLine(). This is because it seems you want to use the input as part of your error message, and nextInt() won't let you do that.
  • We can now move the i declaration inside the try block, along with the code that uses it, so we won't issue "1 was not selected" when the input was "ssss" or whatever.
  • We use NumberFormatException, as that's what Integer.parseInt() throws when it can't parse an integer from the String.
Sign up to request clarification or add additional context in comments.

5 Comments

that mostly works, but for the catch part it gives an error before compiling stating that 'i cannot be resolved to a variable'. How would i fix that?
@user3586248 you need to declare it outside of the try/catch. Right under the line Scanner scan
Yes, if you want to use the input in the exception println, it needs to be declared outside the try block... I'll change the answer to reflect that, though I wouldn't normally do that, as the InputMismatchException has a getMessage() which you could use instead. (You may not like the message it gives, so changing it is fine, if you want).
i tried what you edited it to but now it gives an error: 'the local variable i may have not been initialized'
wait... actually, you can't do this. i will only populate if it's an integer. If you absolutely want to use the input in the error message, you'll need to use a string and parseInt.
0

This is what i mean

while (true) {

    Scanner sc = new Scanner(System.in);
    int i = -1;
    try
    {
        i = sc.nextInt();
    } 
    catch (InputMismatchException e)
    {System.out.println("Sorry, " + i + " is not a number.");}

    if (i == 1)
        System.out.println("1 was selected");
    else
        System.out.println("1 was not selected");
}

Comments

0

I am new to the programming, I think I got the code for your question.

while (true) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        try{
           int i = Integer.parseInt(s);
           if (i == parseInt(i, 3)) {
               System.out.println(s+" is selected");
           } else {
               System.out.println("Input value is " + s);
           }
        } catch (NumberFormatException e) {
            System.out.println("Sorry, " + s + " is not a number.");
        }}}
        private static int parseInt(int i, int j) {
        // TODO Auto-generated method stub
        return 0;
    }}

reference

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.