For Programmers: Free Programming Magazines  


Home > Archive > Java Help > March 2004 > Casting to Integer on Stack, classcastexception









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Casting to Integer on Stack, classcastexception
Jeffrey Spoon

2004-03-27, 12:30 am



Hello, I'm getting a ClassCastException and I'm not sure why. The code
for the casting - op1 = ((Integer)newStack.pop()).intValue(); - as I
understand it, is supposed to cast the object returned by pop to an
Integer object and then the actual primitive value is grabbed by
intValue and assigned to the in op1. Or have I got that wrong? Here's
the full listing:

Tokenise.java:

import java.util.*;




public class Tokenise extends Stack {


protected Object item;
protected Object item2;





public int evaluate (String expr) {

int op1, op2, result = 0;
String token;
StringTokenizer tokenizer = new StringTokenizer (expr);


Stack newStack = new Tokenise(); // evaluation stack

while (tokenizer.hasMoreTokens())
{
token = tokenizer.nextToken();

if (isOperator(token) == true)
{
//...pop top two
operands here then operate on them


op1 = ((Integer)newStack.pop()).intValue();
op2 = ((Integer)newStack.pop()).intValue();

System.out.println("op1" + op1);
System.out.println("op2" + op2);






}else{
//...push operand onto stack
System.out.println("pushing: " +
newStack.push(token));

}




}

//look at top of stack

System.out.println("p: " + newStack.p());

return result; //...

} // method evaluate



public boolean isOperator(String token) {



if (token.equals("+")){
return true;

}else if (token.equals("-")){
return true;

}else if (token.equals("*")){
return true;
}else if (token.equals("/")){
return true;
}

return false;


}// method isOperator










public boolean empty() {


return true;

}//method empty


public Object p() throws EmptyStackException {







return super.p();

}//method p


public Object push(Object item) {


return super.push(item);

// System.out.println("itempush: " + item);



}//method push



public Object pop() throws EmptyStackException {

return super.pop();

//System.out.println("itempop");
//return item;

}// method pop



public int search(Object obj) {





return 0; //for now.

}//method search


} // class Tokenise


Main.java:


public class Main {

public static void main(String [] args) {

Tokenise tok = new Tokenise();
tok.evaluate("7 4 -3 * 1 5 + / *");



} // method: main

} // class: Main


--
Jeffrey Spoon

Lothar Kimmeringer

2004-03-27, 12:30 am

On Thu, 25 Mar 2004 20:58:56 +0000, Jeffrey Spoon wrote:

> Hello, I'm getting a ClassCastException and I'm not sure why.


You push a String on the stack and cast it to Integer when
getting it back. This lead to the exception. You have to
create an Integer out of the String (e.g. with new Integer(token))
and push that instead.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: spamfang@kimmeringer.de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
Eric Sosman

2004-03-27, 12:30 am

Jeffrey Spoon wrote:
>
> Hello, I'm getting a ClassCastException and I'm not sure why. [...]


Because you push each operand onto the stack as a String
object. When an operator comes along, you pop them off and
try to cast them to Integer objects. Strings aren't Integers.

--
Eric.Sosman@sun.com
Jeffrey Spoon

2004-03-28, 10:36 pm

In message <ahuxuoaa4q8w.dlg@kimmeringer.de>, Lothar Kimmeringer
<news200403@kimmeringer.de> writes
>On Thu, 25 Mar 2004 20:58:56 +0000, Jeffrey Spoon wrote:
>
>
>You push a String on the stack and cast it to Integer when
>getting it back. This lead to the exception. You have to
>create an Integer out of the String (e.g. with new Integer(token))
>and push that instead.
>
>
>Regards, Lothar


Doh... Thanks.



--
Jeffrey Spoon

Jeffrey Spoon

2004-03-28, 10:36 pm

In message <406359B9.7626D734@sun.com>, Eric Sosman
<Eric.Sosman@sun.com> writes
>Jeffrey Spoon wrote:
>
> Because you push each operand onto the stack as a String
>object. When an operator comes along, you pop them off and
>try to cast them to Integer objects. Strings aren't Integers.
>



Yep, that'll be why then. Cheers.



--
Jeffrey Spoon

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com