I am trying to build a calculator that will evaluate expressions read from a string in java.
My algorithm recursively splits the input string by its lowest precedence operation (i.e. right-most +) and then evaluates when it is left with a binary operation.
For some reason I am having trouble checking whether or not an operator is nested within parenthesis.
This is my check - exp is the string of the expression, lastMD is the index at which the operator in question is in exp
if (exp.lastIndexOf('(', lastMD) != -1 && exp.lastIndexOf('(', lastMD) < lastMD && exp.lastIndexOf('(', lastMD) > exp.lastIndexOf('(', lastMD)) {
// it is in parenthesis
}
else {
// it is not in parenthesis
}
For some reason it is not working and jumping to the else even when lastMD is contained by parenthesis.
What am I missing?
Thanks!