I was able to add a try catch that tells the user that they cant use letters.However for some reason adding a try catch for negative numbers dosent seem to work.I know that the try block is where if somthing can go wrong like entering in a negative number the catch can print out the error message. I think thats where my problem lies. Another problem that is associated with the try catch is that I'm use to the user entering in -1 to enter the contents that the user inputs so I'm thinking its gonna cause a logical problem.
tl;dr Adding a try catch or another catch to prevent user from adding negative numbers
this is not the the whole program but what it does is that it filters out the integers that the user inputs and separates the evens and odds.
public static void main(String [] args)
{
Scanner stdin = new Scanner(System.in);//for user input
int[] evenNum = new int [100];//Even Array up too 100
int[] oddNum = new int[100];//Odd Array up too 100
int evenIndex=0;//even numbers
int input=0;//user input
int i=0;//incrementer for arrays
int k=0;
int j=0;
String name;
System.out.println("Type In Your Name");//Type in name
name = stdin.nextLine();
while ((i < oddNum.length && i < evenNum.length) && input !=-1)//100 numbers only
{
try{//this is what we want anything else the catch will block it and display a message
System.out.println(name+" Enter a positive number, Enter -1 For results");
input= stdin.nextInt();
oddNum[i]=input;//holds input
i++;//Increments array
}
catch(Exception d){
System.out.println("Only Positive Numbers & no Letters Please!");
stdin.next();
}
}
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.
来源:https://stackoverflow.com/questions/35604342/im-trying-to-add-a-try-catch-that-tells-the-user-they-cant-plug-in-negative-numb