Infix evaluator returning zero as the result everytime. Need Help
I don't know if the evaluator actually works correctly because it always returns a result of zero. I have been messing with it for a while and can't seem to figure out why it won't give any other value than zero for the result. Thanks ahead of time for the help.
Code:
import java.util.Scanner;
public class InFixEvaluator
{
public static int evaluate(String expression)
{
BoundedStackInterface<String> opStack = new ArrayStack<String>(50);
BoundedStackInterface<Integer> valueStack = new ArrayStack<Integer>(50);
String thisOp;
int value;
int operand1;
int operand2;
int result = 0;
Scanner tokenizer = new Scanner(expression);
while (tokenizer.hasNext()){
if (tokenizer.hasNextInt())
{
value = tokenizer.nextInt();
valueStack.push(value);
}
else{
thisOp = tokenizer.next();
opStack.push(thisOp);
}
}
while(!opStack.isEmpty()){
thisOp = opStack.top();
opStack.pop();
if (thisOp == ")")
while(thisOp != "("){
operand1 = valueStack.top();
valueStack.pop();
operand2 = valueStack.top();
valueStack.pop();
thisOp = opStack.top();
opStack.pop();
// Perform operation.
if(thisOp.equals("*")){
result = operand1 * operand2;
valueStack.push(result);
}
else
if(thisOp.equals("+")){
result = operand1 + operand2;
valueStack.push(result);
}
else
if(thisOp.equals("-")){
result = operand1 - operand2;
valueStack.push(result);
}
else
System.out.println("Invalid Operator");
}
if(opStack.isEmpty())
break;
else {
operand1 = valueStack.top();
valueStack.pop();
operand2 = valueStack.top();
valueStack.top();
thisOp = opStack.top();
opStack.pop();
if(thisOp.equals("*"))
result = operand1 * operand2;
else
if(thisOp.equals("+"))
result = operand1 + operand2;
else
if(thisOp.equals("-"))
result = operand1 = operand2;
else
System.out.println("Invalid operator");
valueStack.push(result);
}
result = valueStack.top();
}
return result;
}
}
That is the evaluator and here is the code main class.
Code:
import java.util.Scanner;
public class IFixConsole {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String line;
String more;
int result;
do{
System.out.println("Enter a infix expression to be evaluated: ");
line = input.nextLine();
result = InFixEvaluator.evaluate(line);
System.out.println();
System.out.println("Result = " + result);
System.out.println();
System.out.println("Evaluate another expression? (Y = Yes): ");
more = input.nextLine();
System.out.println();
}
while (more.equalsIgnoreCase("y"));
System.out.println("Program completed.");
}
}
Re: Problem with infix evaluator?
There's a lot going on in that evaluate() method. Sprinkle in some System.out.println( ... ) statements and print the current stack top, operators and operands so you can see what's actually happening in there.
kind regards,
Jos
Re: Problem with infix evaluator?
Thanks I didn't even think about trying that.
Re: Problem with infix evaluator?
Code:
import java.util.Scanner;
public class InFixEvaluator
{
public static int evaluate(String expression)
{
BoundedStackInterface<String> opStack = new ArrayStack<String>(50);
BoundedStackInterface<Integer> valueStack = new ArrayStack<Integer>(50);
String thisOp;
int value;
int operand1;
int operand2;
int result = 0;
Scanner tokenizer = new Scanner(expression);
while (tokenizer.hasNext()){
if (tokenizer.hasNextInt()){
value = tokenizer.nextInt();
valueStack.push(value);
}
else{
thisOp = tokenizer.next();
opStack.push(thisOp);
}
}
while(!opStack.isEmpty()){
thisOp = opStack.top();
opStack.pop();
if (thisOp == ")"){
while(thisOp != "("){
operand1 = valueStack.top();
valueStack.pop();
System.out.println(operand1);
operand2 = valueStack.top();
valueStack.pop();
System.out.println(operand2);
thisOp = opStack.top();
opStack.pop();
System.out.println(thisOp);
if(thisOp.equals("*")){
result = operand1 * operand2;
valueStack.push(result);
}
else
if(thisOp.equals("+")){
result = operand1 + operand2;
valueStack.push(result);
}
else
if(thisOp.equals("-")){
result = operand1 - operand2;
valueStack.push(result);
}
}
}
else {
operand2 = valueStack.top();
valueStack.pop();
operand1 = valueStack.top();
valueStack.pop();
if(thisOp.equals("*"))
result = operand1 * operand2;
else
if(thisOp.equals("+"))
result = operand1 + operand2;
else
if(thisOp.equals("-"))
result = operand1 - operand2;
valueStack.push(result);
}
result = valueStack.top();
}
return result;
}
}
That is what I have so far and it works when there is no parentheses. I need it to work with parentheses and I need the operators to work in the correct precedence which I am having trouble figuring out how to do that.