Im trying to add a try catch that tells the user they cant plug in negative numbers

僤鯓⒐⒋嵵緔 提交于 2019-12-04 17:12:59

You can check the input variable after you get it from the scanner

if (input < 0) {
     System.out.println("Only Positive Numbers & no Letters Please!");
}

Your code does not throw any Exception when the number is read from the scanner. So you cannot expect that the execution jumps into the catch-block when you enter a negative number.

But you can alternatively throw an exception when the input is negative. This will make the thread to jump directly into the catch-block. In the catch-block you can then print the message you passed the IllegalArgumentException

if (input < 0) {
     // this gets caught in the catch block
     throw new IllegalArgumentException("Only Positive Numbers & no Letters Please!"); 
}      
...
} catch (IllegarArgumentException e) {
    System.out.println(e.getMessage());
}

It is generally bad practice to catch Exception (java.lang.Exception). This is the "root" of all checked exceptions and the catch-block will be jumped into whenever any subclass of Exception is thrown.
Just catch the concrete exception that you are expecting. (In this case IllegalArgumentException.)

Also you should not use exceptions to control the execution flow of your program.

I would suggest something like this:

do {
    System.out.println(name+" Enter a positive number, Enter -1 For results");
    try {
        input = stdin.nextInt();
    } catch (java.util.InputMismatchException e) { // if the user enters something that is not an integer
        System.out.println("Please only enter integers");
        input = Integer.MIN_VALUE; 
        stdin.next(); // consume the non-int so we don't get caught in an endless loop
    }
} while (input < -1);  // loop as long as the input is less than -1

if (input == -1) {
    // show the results here
}

This will accept positive integers and will prompt for an input until the user enters a positive number, 0 (zero) or -1 (which should show the results)

In order for your catch block to catch exception, the Exception needs to be thrown from the code. In case of negative numbers the line input= stdin.nextInt(); will not throw exception as it is perfectly legal for integer to be negative. You will need to add if condition like this:

input = stdin.nextInt();
if ( input < 0 ) {
  throw new Exception("Negative number entered");
}

But some consider this to be bad practice because you are using exceptions to control the flow of a program. So I give you another example how you can do this without throwing an exception:

input = stdin.nextInt();
if ( input < 0 ) {
  System.out.println("Only Positive Numbers Please");
  continue;  // will continue from the beginning of a loop
}

You can do it like this:

if (input < 0) {
    throw new IllegalArgumentException();
}

Now if the number is negative, it will throw exception and the catch code can be executed. Because you catch Exception so all of the exception will be catches here.

Note: In catch block you no need to add stdin.next(); because the program will continue from the first line of while loop.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!