1

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!

5
  • What is the "different error" being shown? Commented Dec 3, 2019 at 18:55
  • Post all your code and the error you get Commented Dec 3, 2019 at 18:56
  • You have defined your own Exception type, but not thrown it (most likely). The NumerFormatException which gets thrown when parsing numbers is an internal class. You will probably need to delete your own NumberFormatException class, and deal with System.out.println-ing the alert you need when that gets thrown. Commented Dec 3, 2019 at 19:01
  • Can you please post the "different error" you're getting? Commented Dec 3, 2019 at 19:07
  • What's on Calculator.java line 59? Commented Dec 3, 2019 at 19:09

1 Answer 1

1

Your Exception is ArrayIndexOutOfBoundsException . This is not related to the format of Strings. It is about trying to access array elements that do not exist.

Look at line 59 as the message indicates. You haven't shared this with us, so you're on your own from here.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.