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.

Recommended Answers

All 3 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.