This is from my early expression evaluator, which takes an infix expression like yours and turns it into postfix for evaluation. There are methods that help the parser, but I think they are pretty self-documenting. Mine uses character tables to check markers. It also allows you to use user-defined symbols and nested assignments and other things that you may not need / need. But it shows how I dealt with your problem without using subtleties like regex, which would simplify this task. In addition, everything shown is my own implementation - the stack and the queue - that's it. So if something looks abnormal (unlike Java imps), it is because it is.
This section of code is important in order not to answer your immediate question, but to show the necessary work, to determine the type of marker you are dealing with. In my case, I had three different types of operators and two different types of operands. Based on the well-known rules or rules that I chose to enforce (when necessary), it was easy to find out when something was a number (starts with a number), a variable / user symbol / mathematical function (starts with a letter), or a mathematical operator (is: /, *, -, +). Note that only the first char is required to get the correct extraction rules. From your example, if all your cases are simple, you will only need to process two types: operator or operand. However, the same logic will apply.
protected Queue<Token> inToPostParse(String exp) {
Basically I have a stack (s1) and a queue (q1). All variables or numbers are queued. Any trig, math, parens, etc. operators Go to the stack. If the current token needs to be pushed onto the stack, you should check the status (at the top) to determine what parsing action to take (i.e., what to do based on mathematical priority). Sorry if this seems like useless information. I assume that if you parse a mathematical expression because at some point you plan to evaluate it. IMHO, the postfix is ββthe easiest, so I, regardless of the input format, change it to a post and evaluate it using one method. If your O is different, do what you like.
Edit: Implementations
The statement formula and numerical methods that interest you most are as follows:
protected String extractPhrase(int it) { String phrase = new String(); char c; for ( ; it < inputExp.length(); ++it) { c = inputExp.charAt(it); if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) { phrase += String.valueOf(c); } else { break; } } return phrase; } protected String extractNumber(int it) throws NumberFormatException { String number = new String(); int decimals = 0; char c; for ( ; it < strLength; ++it) { c = inputExp.charAt(it); if (c >= '0' && c <= '9') { number += String.valueOf(c); } else if (c == '.') { ++decimals; if (decimals < 2) { number += "."; } else { throw new NumberFormatException(); } } else { break; } } return number; }
Remember - by the time of input of these methods I was already able to deduce what type. This avoids the endless chain of while-if-else.