2

Ok so I have a method with a switch statement but I left out the rest of the cases because they're not important. In my main method, the operator method is called and passed the parameter "selection" in a while loop until they choose "Q".

When the user enters a negative number, it should throw an exception, print a message, and ignore their input but then loop back to the beginning. When this exception is thrown it terminates the program. Any help would be very much appreciated. Thanks!

public static void operator(String selection) throws IllegalArgumentException{
    Scanner input = new Scanner(System.in);
    double price;
switch(selection){
case "A":
     System.out.println("Enter the price");
        if(input.nextDouble()<0){
            throw new IllegalArgumentException("Price cannot be a negative value");
        }
        else{
            price = input.nextDouble(); 
        }
break;

case"Q":
  System.exit(0);
}
}
Nick Castello
  • 51
  • 2
  • 9

3 Answers3

4

An IllegalArgumentException inherits from RuntimeException, for it not to stop your program you can just use a simple try{} catch {} but i don't recommend doing that with Runtime Exceptions. If that's the case, create your own Exception inheriting from java.lang.Exception.

You can use try catch here.

Something like this should work:

public static void operator(String selection) {
Scanner input = new Scanner(System.in);
double price;
switch(selection){

case "A":
 System.out.println("Enter the price");
     try {
        if(input.nextDouble()<0) {
            throw new NegativePriceException();
        }
     } catch (NegativePriceException e) {
        System.out.println("The price can't be negative.");
        e.printStackTrace();
     }

    price = input.nextDouble(); 
    break;

case"Q":
  System.exit(0);
}
}

And to make your own Exception class you basically need to inherit from Exception (if you want to use try catch on it) or inherit from RuntimeException (if you want it to stop your program from running), like this:

public class NegativePriceException extends Exception {

  public NegativePriceException() {
     super();
  }
}
Julian
  • 314
  • 1
  • 10
  • Thank you so much for getting back so quickly. This does work, however, I'm required to throw an exception if the input is invalid. I think creating a custom exception would be my best bet. Could you steer me in the right direction as far as making that goes? – Nick Castello Jul 22 '16 at 18:02
  • Added how to create your own exception and edited code to use try catch instead of while. – Julian Jul 22 '16 at 18:16
  • No problem man, just evaluate the answer that's been the most helpful to you. – Julian Jul 22 '16 at 18:29
1

Java requires that you handle or declare all exceptions. If you are not handling an Exception using a try/catch block then it must be declared in the method's signature.

In your main method you should handle the Exception

public static void main(String args[]) {
//codes
try{
operator("A");
}
catch(IllegalArgumentException e){
e.printStackTrace();
}
}
Jinu P C
  • 2,390
  • 16
  • 26
0

You've gotten some good answers already on how to handle the exception.

For your case I don't think an exception is appropriate at all. You should get rid of the exception altogether and just handle the problem input by printing an error message and asking for a new input.

Exceptions are for exceptional situations, they should not be part of the normal execution of your code.

puhlen
  • 7,607
  • 1
  • 13
  • 28