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)

[12168 byte] By [hyetecka] at [2007-10-2 5:49:35]
# 1
Hi,The problem is that you are pushing the result as an Integer object, but you are always expecting the value to be a String when you pop. Change your code so that you always push strings.Kaj
kajbja at 2007-7-16 1:59:02 > top of Java-index,Java Essentials,Java Programming...
# 2
awesome..thanks so much..this is what i did so that it works now.evalStack.push(Integer.toString(value));
hyetecka at 2007-7-16 1:59:02 > top of Java-index,Java Essentials,Java Programming...
# 3

I added a small main() method to test your code:

public static void main(String[] args) throws Exception {

InfixToPostfix x = new InfixToPostfix();

System.out.println(x.evaluate("4 2 3 + -"));

}

and got this:

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer

at InfixToPostfix.evaluate(InfixToPostfix.java:134)

at InfixToPostfix.main(InfixToPostfix.java:12)

The problem is that you have put a java.lang.Integer object on the stack, but in line 134 (or line 117 in your version of the code) you are getting an object off the stack and you cast it to a String.

You can't cast an Integer to a String.

jesperdja at 2007-7-16 1:59:02 > top of Java-index,Java Essentials,Java Programming...
# 4
Too late! .... :-)
jesperdja at 2007-7-16 1:59:02 > top of Java-index,Java Essentials,Java Programming...