1

I have an InputMismatchException which stops a decimal from being entered, but it doesn't help for negative integers/negative decimals.

if(userInput == 1) {

            int l;

            l = 0;

            try {

                l = input.nextInt();


            } catch (InputMismatchException e) {

                System.out.println("");
                input.next();

            } 

        }

If I add a do while loop with an if statement for anything equal to or less than zero it will loop within the if(userInput == 1) statement instead of starting from the beginning of the menu like it does if a positive decimal is entered. It also doesn't help for negative decimals.

I've tried to add two exceptions to the catch, but can't get that to work.

anyset
  • 43
  • 2
  • 7
  • 1
    *it will loop within the if(userInput == 1) statement* an `if` is **not** a `loop` – Scary Wombat Mar 16 '17 at 02:41
  • This might help http://stackoverflow.com/questions/35604342/im-trying-to-add-a-try-catch-that-tells-the-user-they-cant-plug-in-negative-numb – kalenpw Mar 16 '17 at 02:41
  • There could still be content in the buffer, either that or `userInput` needs to be reset, but since you've not graced us with a working example of your problem, it's impossible to know what to suggest – MadProgrammer Mar 16 '17 at 02:41
  • see this possible duplicate http://stackoverflow.com/questions/19950713/scanner-input-validation-in-while-loop and [this](http://stackoverflow.com/a/19130231/2310289) – Scary Wombat Mar 16 '17 at 02:42
  • one of the set of possible solutions: add method readInt throws InputMismatchException to your code, inside do `l = input.nextInt();` and then validate it's > 0 and throw InputMismatchException if it's not – borowis Mar 16 '17 at 02:43

2 Answers2

0

Try this:

if (input < 0){
      throw new IllegalArgumentException("Input cannot be negative.");
}

If the number is negative, it will throw the exception and then the catch code can be executed.

NewBie1234
  • 461
  • 6
  • 23
  • When I add that to the try statement it doesn't execute the catch code. Here is what I've put. `try { l = input.nextInt(); if (l < 0){ throw new IllegalArgumentException("Input cannot be negative."); }` – anyset Mar 16 '17 at 02:52
  • 1
    @anyset - Obviously! What you need to do is understand this suggestion, and adapt it to your context. If you are going to throw an exception, throw an exception **that you catch**. But, frankly, throwing an exception is not the right solution ... given the way that you are currently handling the other exception. The key is to try to understand what exceptions / exception handling offer, and use them (or not!) appropriately. (Hammers are not good screwdrivers) – Stephen C Mar 16 '17 at 03:05
  • I agree! - instead of throwing an exception you could is simply refuse any negative input – NewBie1234 Mar 16 '17 at 03:10
  • This all makes sense. Now it refuses negative input. Thank you. – anyset Mar 16 '17 at 03:25
0

You could just turn your int into a String and check if it has a decimal point or a negative sign.

Scanner input = new Scanner(System.in);
    int l;
    while (true)
    {
        l = input.nextInt();
        if ((""+l).indexOf('.') >= 0 || (""+l).indexOf('-') >= 0)
        {
            System.out.println(/*your message here*/);
        }
        else
        {
            break;
        }
    }

//continue with your program

Edit: Syntax

beninato
  • 420
  • 5
  • 15