It will still return an error with this as it will go to the default case for 9+9. I’d recommend implementing a string searching algorithm before using parseint to then do the + operation
The thing is that this kind of calculator is only allowed to take in inputs like +99, not 9+9, even though the second one makes more sense. Sorry if I sound a bit annoying.
It might be easier to reverse the input string first. (ex. if the user inputs +3+45, use a string reversing algorithm to give you 54+3+) this will make it easier to parse inputs with multiple operations, since its in the correct order of operations now. For +3+45 = 54+3+, 54+ = 5+4 = 9, then 93+ = 9+3 = 12. If you were in the original notation, you'd have to add extra logic in your string parsing to find which operation to do first.
When its reversed, you can simply take the first two characters of the string, (in ex above that's 5 and 4) make sure they're valid numbers 0-9, then perform the operation that is after them (+). Return the result as your first digit (9), then read the second digit (3) and the operation after it (+). Perform the operation and continue the above until you reach the end of the String, at which point you print the result to the console.
The hardest part is the String parsing, but if you get that right, isolating the operations and the digits, it'll be a lot easier to work with.
9
u/jubiman Nov 13 '20
Just use Interger.parseInt(str);