I’m working on a calculator project in my free time to keep up my Java skills during the summer, but I have run into a snag. So far everything is working great and it’s basically done; I’m just trying to add some functionality.
I’m trying to figure out how I can set it up so that instead of typing 5 then clicking +, -, /, or * and then typing the second number, how to let the user type number1(operator)number2. I’m mostly just looking to try and figure out how to take everything before the operator or key string and then everything after it and make them both separate strings, then I can parse them and add them into the rest of my code. I simply forgot how to get everything before the + and after as separate things! I’m assuming I will use indexOf() and substrings.
Yes, use
indexOfandsubstring(assuming this is a simple calculator that does not allow parenthesis or nesting – if it is more complex then use a parser).substringhas two forms that will be useful to you:Remember that
endIndexis exclusive, so it will not include the index specified.