I am working on a Calculator project for my java class that requires me to use exceptions. When an operator is entered into the calculator followed by anything other than a number, it should catch the NumberFormatException and display an error message saying "Invalid non-numeric operand." Instead, it shows a different error.
This is the code I have to catch the error:
catch (NumberFormatException e) { System.out.println(e.getMessage()); input = kb.nextLine(); } And I have this to display the message:
public class NumberFormatException extends IllegalArgumentException { public NumberFormatException() { super("Invalid non-numeric operand."); } } And this is the error I get:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at Calculator.doCalculation(Calculator.java:59) at CalculatorTest.main(CalculatorTest.java:25) But I am unsure of what to do from here as I don't quite understand the exception. I apologize if this is simple, I am very new to java. Please help, thank you!
Exceptiontype, but not thrown it (most likely). TheNumerFormatExceptionwhich gets thrown when parsing numbers is an internal class. You will probably need to delete your ownNumberFormatExceptionclass, and deal withSystem.out.println-ing the alert you need when that gets thrown.