i'm having a problem with my code for evaluating postfix expressions. any comments on what's wrong on my code?

import java.io.*;
import java.util.*;
import java.util.Stack;

public class Eval
{
  private Stack<String> operatorStack;
  private Stack<Integer> operandStack;

  public  int evaluate(String output)
	{
		StringTokenizer s = new StringTokenizer(output);//divides into tokens

		int value;
		String symbol;
		while (s.hasMoreTokens())
		{
			symbol = s.nextToken();
			if (Character.isDigit(symbol.charAt(0)))// if its a number push it

			{
				Integer operand = new Integer(Integer.parseInt(symbol));
				operandStack.push(operand);
			}
			else // if it's an operator, operate on the previous two popped operandStack items
			{
				int op2 = ((Integer)operandStack.pop()).intValue();
				int op1 = ((Integer)operandStack.pop()).intValue();
				int result = 0;
				switch(symbol.charAt(0))
				{
					case '*': {result = op1 * op2; break;}
					case '+': {result = op1 + op2; break;}
					case '-': {result = op1 - op2; break;}
					case '/': {result = op1 / op2; break;}
					case '%': {result = op1 % op2; break;}
				}
				Integer operand = new Integer(result);
				operandStack.push(operand);
			}
		}
		value = ((Integer)operandStack.pop()).intValue();
		return value;
	}

	public static void main(String[] args)
	{
		Eval app = new Eval();
		Scanner scan = new Scanner(System.in);
		System.out.println("Input postfix expression: ");
		String output1 = scan.nextLine();
		app.evaluate(output1);
	}

}

Edited 5 Years Ago by ~s.o.s~: Added code tags, please learn to use them.

You have not initialized your operand stack and you don't do anything with the value returned by the "evaluate" method.

That being said, don't use StringTokenizer but use the "split" method of the String class. Also, Stack class extends Vector class which has all its method synchronized and has fallen out of use except in legacy cases. If you are using Java 6, use a LinkedList class or ArrayDeque class exposed by the Dequeue interface.

hey thanks for editing my post.. i'll try to study using the "split" method and LinkedList.. but i got another question.. am i doing it correctly using the switch case in evaluating with the operators?

btw i really need to figure this out.. cuz i got a deadline for my Calculator project in 2days.. I already managed to make my codes for converting infix to postfix.. now i am having this problem with evaluating the postfix.. so please can someone help me with this!!!

now i am having this problem with evaluating the postfix

*What* kind of problem? You need to be more detailed when posting problem statements rather than just saying you've got a problem.

BTW, the code isn't pretty as far as error checking is concerned. You only test for the first character of a token to verify its correctness. As per your logic, the following postfix expression becomes correct: 1 2 +? and yields 3 but it shouldn't. Similarly, the expression 1b 2 + fails in an unexpected manner.

This article has been dead for over six months. Start a new discussion instead.