Skip to main content
gamedev.net
🔒 Locked

Help with basic Java error

Started by Pfhorseti 3 replies 250+ views
Original Post
Pfhorseti
Pfhorseti
I am getting this error from the compiler, I wonder why? Uranai.java:84: cannot resolve symbol symbol : method intValue () int left = oldStack.pop().intValue(); What I''m trying to do here is pop the stack, get the Integer object out and call it intValue(), which should return the int value of the Integer object and put it into int left. I tried this: int left = (Integer)oldStack.pop().intValue(); But then it would say incompatible types. What am I doing wrong here?
-forseti
jonnii
jonnii
i think you can only store objects in the stack, not basic types. so you''ll have to insert an int wrapped as an Integer... something like new Integer(myInt).

then when you pop it off you can go

int myVal = (Integer).stack.pop().intValue();

probably.
-jonnii=========jon@voodooextreme.comwww.voodooextreme.com
stubble
stubble
I wouldn''t really class myself as a Java programmer, but as this line of code could be pretty much Java or C++..

int left = (Integer)oldStack.pop().intValue();

fails because you are casting the result of the expression to an Integer object and then trying to assign that to an int - which are different types.

You need to cast the result of oldStack.pop() to and Integer and then call intValue() - which will make the type of the expression an int - which you can assign to your left variable - ie.

int left = ((Integer)oldStack.pop()).intValue();

Pfhorseti
Pfhorseti
Ahh, thank you very much.
I assumed the (Integer) cast would apply before the .intValue().
-forseti
stubble
stubble
Yes that''s right. You''re casting the return value of oldStack.pop() before you call a method on it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.