Help with CastException please
Hi,
i am working on program that converts a string from infix notation to postfix notation and then evaluates it. I am having a problem with the evaluation part.
lets say we have 2 + 4 - 3
the postfix will be 2 4 + 3 -
The program does 2 + 4 and pushes the result onto the stack. When it gets to the - and tries to do 6 - 3, that is where the problem is. WHen i pushed the first result back onto the stack i am pusing an Integer Object. When i am popping, i am converting it to an int by parsing it. I think this is where the error is, but i am not sure.
here is my code below. Its very long, but the part where the error is happening is in the evaluate method.
import java.io.*;
import javax.swing.*;
import java.util.*;
class InfixToPostfix
{
private Stack theStack;
private String postfix;
public InfixToPostfix()
{
postfix=" ";
}
public String toPostFix(String exp)
{
StringTokenizer tokLine =new StringTokenizer(exp);
theStack =new Stack(tokLine.countTokens());
while (tokLine.hasMoreTokens())
{
String token = (String)tokLine.nextToken();
if(postfix.charAt(0)!='S')
{
if(token.equals("+") || token.equals("-"))
gotOperator(token, 1);
elseif(token.equals("/") || token.equals("*") || token.equals("%"))
gotOperator(token, 2);
elseif(token.equals("(")==true)
theStack.push(token);
elseif(token.equals(")")==true)
gotParen();
else
postfix = postfix + token +" ";
}
}
while (!theStack.isEmpty())
{
if(postfix.charAt(0)!='S')
postfix = postfix + theStack.pop() +" ";
}
return postfix;// return postfix
}
publicvoid gotOperator(String op,int prec)
{
if(theStack.isEmpty()==false)
{
String opTop = (String)theStack.peek();
int precTop;
if(opTop.equals("+") || opTop.equals("-"))
precTop = 1;
else
precTop = 2;
if(opTop.equals("("))
theStack.push(op);
else
{
if(prec > precTop)
theStack.push(op);
if(prec <= precTop)
{
opTop = (String)theStack.pop();
postfix = postfix + opTop +" ";
theStack.push(op);
}
}
}
else
theStack.push(op);
}
publicvoid gotParen()
{
if(theStack.isEmpty()!=true)
{
String opTop = (String)theStack.peek();
while(opTop.equals("(")!=true && theStack.isEmpty()!=true)
{
opTop = (String)theStack.pop();
postfix = postfix + opTop +" ";
opTop = (String)theStack.peek();
}
if(opTop.equals("("));
opTop = (String)theStack.pop();
}
else
postfix ="Sytanx Error. Extra right parenthesis";
}
public Object evaluate(String postfix)
{
StringTokenizer tokLine =new StringTokenizer(postfix);
Stack evalStack =new Stack(tokLine.countTokens());
int value = 0;
int operand2, operand1;
String item,result=" ";
while(tokLine.hasMoreTokens())
{
item = (String)tokLine.nextToken();
if(item.equals("+"))
{
if(evalStack.isEmpty()!=true)
{
operand2 = Integer.parseInt((String)evalStack.pop());
if(evalStack.isEmpty()!=true)
{
operand1 = Integer.parseInt((String)evalStack.pop());
value = operand1 + operand2;
Integer pushValue =new Integer(value);
evalStack.push(pushValue);
}
else
result ="Error: Not enough operands.";
}
else
result ="Error: Not enough operands.";
}
elseif(item.equals("-"))
{
if(evalStack.isEmpty()!=true)
{
operand2 = Integer.parseInt((String)evalStack.pop());
if(evalStack.isEmpty()!=true)
{
operand1 = Integer.parseInt((String)evalStack.pop());
value = operand1 - operand2;
Integer pushValue =new Integer(value);
evalStack.push(pushValue);
}
else
result ="Error: not enough operands.";
}
else
result ="Error: not enough operands.";
}
elseif(item.equals("*"))
{
if(evalStack.isEmpty()!=true)
{
operand2 = Integer.parseInt((String)evalStack.pop());
if(evalStack.isEmpty()!=true)
{
operand1 = Integer.parseInt((String)evalStack.pop());
value = operand1 * operand2;
Integer pushValue =new Integer(value);
evalStack.push(pushValue);
}
else
result ="Error: not enough operands.";
}
result ="Error: not enough operands.";
}
elseif(item.equals("/"))
{
if(evalStack.isEmpty()!=true)
{
operand2 = Integer.parseInt((String)evalStack.pop());
if(evalStack.isEmpty()!=true)
{
operand1 = Integer.parseInt((String)evalStack.pop());
value = operand1 / operand2;
Integer pushValue =new Integer(value);
evalStack.push(pushValue);
}
else
result ="Error: not enough operands.";
}
else
result ="Error: not enough operands.";
}
elseif(item.equals("%"))
{
if(evalStack.isEmpty()!=true)
{
operand2 = Integer.parseInt((String)evalStack.pop());
if(evalStack.isEmpty()!=true)
{
operand1 = Integer.parseInt((String)evalStack.pop());
value = operand1 % operand2;
Integer pushValue =new Integer(value);
evalStack.push(pushValue);
}
else
result ="Error: not enough operands.";
}
else
result ="Error: not enough operands.";
}
else
evalStack.push(item);
}
Object finalValue =new Object();
finalValue = evalStack.pop();
if(evalStack.isEmpty()!=true)
result ="Error: Not enough operators.";
if(result.equals(" "))
return finalValue;
return (Object)result;
}
}
can you tell me what i need to do to fix the Cast Exception that i am getting?
this is the error that it is giving.
java.lang.ClassCastException
at InfixToPostfix.evaluate(InfixToPostfix.java:117)

