- #1
muna580
I am evaluating a postfix expression using stacks and stuff. Below is the method I made. But I am not sure where to put the try/catch satement and what is it that i have to catch.
Code:
public int evaluateRPN()
{
StringTokenizer token = StringTokenizer(postfix);
stk.clear();
try
{
while(token.hasMoreTokens())
{
String s = token.nextToken();
if(isNumber(s))
stk.push(new Integer(Integer.parseInt(s)));
else
{
char operator = s.charAt(0);
int x1 = ((Integer)stk.pop()).valueOf();
int x2 = ((Integer)stk.pop()).valueOf();
int x3;
switch(operator)
{
case StackConstants.ADDITION_OPERATOR:
x3 = x2 + x1;
stk.push(new Integer(x3));
break;
case StackConstants.SUBTRACTION_OPERATOR:
x3 = x2 - x1;
stk.push(new Integer(x3));
break;
case StackConstants.MULTIPLICATION_OPERATOR:
x3 = x2 * x1;
stk.push(new Integer(x3));
break;
case StackConstants.DIVISION_OPERATOR:
x3 = x2 / x1;
stk.push(new Integer(x3));
break;
case StackConstants.MODULUS_OPERATOR:
x3 = x2 % x1;
stk.push(new Integer(x3));
break;
}
}
}
}
catch(EmptyStackException e)
{
}
return ((Integer)stk.pop()).valueOf();
}