File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes From object to int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "From object to int" Watch "From object to int" New topic

From object to int

L�szl� Kov�cs

Joined: Jun 18, 2005
Posts: 17
On a couple of occasions I have had data returned in the form of an object that I need to cast into an int. I would like to know a better way to do it than what I have currently:

Disclaimer: The above post may or may not reflect reality as seen by other people.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
just a guess:
why not making getValue() return an int, including Integer.parseInt() in that method?

java amateur
L�szl� Kov�cs

Joined: Jun 18, 2005
Posts: 17
I should probably have mentioned that the object is being returned by a JSpinner - since I didn't make it, I can't very easily have it return a different type.
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130

I don't see too much that you could change with your code - I suppose you could cut it back by a line -

L�szl� Kov�cs

Joined: Jun 18, 2005
Posts: 17
Thanks JD.
Ravi Shankar R

Joined: Jan 05, 2005
Posts: 7
Object oValue = mySpinner.getValue(); // getValue() returns an object

// String sValue = oValue.toString(); Eliminate the conversion of an object to String

int iValue = ((Integer)oValue).intValue();

thus, this would give u the same result unless u r sure that the object returned is of Integer type

[ July 06, 2005: Message edited by: Ravi Shankar R ]
I agree. Here's the link:
subject: From object to int
It's not a secret anymore!