Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Error when casting from object to integer Posted by KompewterFreek on 25 Feb 2005 at 9:11 PM
imported java.lang.*;

I have delcared
Integer L, R;

tried this line:
L=new Integer(theStack.pop());
R=new Integer(theStack.pop());

when compiling i get this error:
cannot find symbol
symbol : constructor Integer(java.lang.Object)
location : class java.lang.Integer
L (or R)= new Integer(theStack.pop());
^

The only thing i can think of that may not be right is the fact that the data stored in the stack is
already type Int, and the casting is looking for type object. But i thought it shouldn't matter because of the higherarchy. What else could be wrong?

Report
Re: Error when casting from object to integer Posted by ronenm on 26 Feb 2005 at 11:08 AM
The class Integer has a constructor.

you can go and look at the source code for this class and you will see that the constructor doesn't except an Object.

you called on the Integer constructor when you did: new Integer(Object)

so the command is not recodnized.
Report
Re: Error when casting from object to integer Posted by KompewterFreek on 26 Feb 2005 at 4:34 PM
i see, so how would i cast an object to integer? i can't figure it out, Integer.parseInt only works for a string parameter. Any suggestions?
Report
Re: Error when casting from object to integer Posted by ronenm on 26 Feb 2005 at 11:36 PM
: i see, so how would i cast an object to integer? i can't figure it out, Integer.parseInt only works for a string parameter. Any suggestions?
:

since you are using a stack and you say the stack returns an object when you pop(), you should know that the compiler will have no idea what is really in the stack, so it is regarded to as some kind of Object.

you will need to tell the compiler: "Trust me man, I know what I'm doing" when you try casting.

so if you want to go over through all the items in the stack you'll do this:

for (all items in the stack)
if (item instanceof integer)
do --> put the Integer into a variable

use the "instanceof" - it tells the compiler to check if the item on the stack is really an instance of an integer, thus allowing you to cast.


Report
Re: Error when casting from object to integer Posted by arb123 on 28 Feb 2005 at 6:27 AM
If your stack contains Objects of type Integer, you can cast using:
Integer n = (Integer) stack.pop();

Of course, if the Object isn't an instance of Integer, a ClassCastException will be thrown.

You can get the type of an object from its class:
System.out.println( o.getClass().getName() );


---------------------------------
HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.