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");

        }
user3586248
  • 163
  • 1
  • 14

3 Answers3

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.
billjamesdev
  • 14,554
  • 6
  • 53
  • 76
  • 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 Nov 10 '15 at 04:10
  • @user3586248 you need to declare it outside of the try/catch. Right under the line `Scanner scan` – 3kings Nov 10 '15 at 04:12
  • 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). – billjamesdev Nov 10 '15 at 04:13
  • i tried what you edited it to but now it gives an error: 'the local variable i may have not been initialized' – user3586248 Nov 10 '15 at 04:16
  • 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. – billjamesdev Nov 10 '15 at 04:18
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");
}
3kings
  • 838
  • 2
  • 13
  • 28
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

Community
  • 1
  • 1
Chinna
  • 1