I'm trying to construct my own expressions evaluator based on the Shunting-yard algorithm. I'm having difficulty adding String
objects to the stack I have though. My code just skips over these lines every time, even when the condition is met. Any suggestions? I have the operatorStack
declared as:
Stack operatorStack = new Stack();
I'm thinking that it has something to do with my if statement. I've tested it with the Eclipse debugger and when the currentChar
variable shows as "("
it still skips over the push to the stack.
Here is the excerpt in question:
int count = 0;
String currentChar=new String();
//While loop to test for the conditions stated in the Shunting-yard algorithm.
while (count<=temp.length()) {
currentChar = temp.substring(count, count+1);
if(currentChar == "(")
operatorStack.push(currentChar);
if(expressionEval(currentChar) instanceof Integer)
outputQueue.offer((Integer)expressionEval(currentChar));
if(currentChar == "+" ||
currentChar == "-" ||
currentChar == "<" ||
currentChar == "?") {
while(operatorStack.peek() == "+" ||
operatorStack.peek() == "-" ||
operatorStack.peek() == "<" ||
operatorStack.peek() == "?") {
outputQueue.offer((Integer)operatorStack.peek());
operatorStack.pop();
}
operatorStack.push(currentChar);
}