hello i am doing my assignment on simple calculator in java.Now I want to handle exceptions in it. so i need some information about exception for exemple a user enter any expression like (10+23+56-55) it is corrent formate but when the user enter wrong formate like (20++56//5) i mean operator in wrong formate so it is also an exception now i want to handle it. i need a built in class that handle this type of exception.

2 Years
Discussion Span
Last Post by BenWard

so, you've told us what you need to do. now, what exactly is your question about it?
get an array of chars from the String, then check each element, and verify that the next element of the char is valid. if not, throw an Exception.


You could make a list of invalid combinations of opertors eg /*, */, ++ etc and stick them in a static final array. Loop through them comparing them to your input with String.contains(). It wouldn't matter if you had +++ entered by the user because ++ will still get picked up there. Also add your non-numeric characters to that array so the user cannot type (29-fred/98)

Saying that, I'd sooner do validation at input stage that checks each character entered as they are entering it.


Don't forget multiplying and dividing by a negative number is perfectly valid so /- is a valid combination of operators.

This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.