1

Hello I'm running this code:

public static int chooseOption() {
    int choice = 0;
    System.out.println("Enter number [1, 2, 3..]");

    do {
        try { 
            choice = sc.nextInt();
        } catch ( java.util.InputMismatchException e ) {
            System.out.println(e);
            break;
        }
    } while( choice == 0 || choice < 0);


    return choice;
}


public static String chooseCom() {
    String choice = new String();
    int commNumber = 0;

    System.out.println("Choose your COM port");
    commNumber = chooseOption();

    choice = "COM" + commNumber;
    System.out.println(choice);
    return choice;
}

after that I call - >

Helper.chooseOption();

Helper.chooseCom();

and when I write on the first call 1.1 or just a wrong float(double) value the output is:

Enter number [1, 2, 3..]
1.1
java.util.InputMismatchException
Choose your COM port
Enter number [1, 2, 3..]
java.util.InputMismatchException
COM0

The second java.util.Input.. is the problem why is he poping when I wrote wrong number only on the first call?

4
  • What is sc? Where is it defined, and what's in it? Commented Mar 29, 2013 at 12:30
  • Please post more of the code you're using as there doesn't appear to be enough here to give a derived answer. Commented Mar 29, 2013 at 12:34
  • 1
    @JohnKugelman looks like a java.util.Scanner Commented Mar 29, 2013 at 12:35
  • I think in both instances the exception has been thrown right.. so what is the abnormal behavior your are referring. When you called Helper.chooseCom() exception has been thrown, loop exited and the method return with 0. This is then printed as 'COM0'. Commented Mar 29, 2013 at 12:41

2 Answers 2

3

When given input that doesn't match what is expected, nextInt doesn't consume the input. From the documentation:

When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.

A solution is skipping over the invalid input in the catch-clause, for example with nextLine:

    try { 
        choice = sc.nextInt();
    } catch ( java.util.InputMismatchException e ) {
        System.out.println(e);
        sc.nextLine(); // skip the entire line
        break;
    }
Sign up to request clarification or add additional context in comments.

Comments

3

Assuming that sc is a java.util.Scanner ...

The Scanner class works by (conceptually) splitting the input into tokens, and then processing the tokens. If you call sc.nextInt() when the next token does not match the syntax for an integer, the call will throw that exception.

However, if a nextXxx method throws an exception, it does not "consume" the next token. Instead, it leaves it in the scanner's token stream ... so when you then call sc.nextInt() again, it will attempt to parse the same token again and throw the same exception.

To discard the token, you need to call sc.next(). Or if you want to discard the entire line, call sc.nextLine().

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.