Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

From object to int

 
L�szl� Kov�cs
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just a guess:
why not making getValue() return an int, including Integer.parseInt() in that method?
 
L�szl� Kov�cs
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laszlo,

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



Regards,
JD
 
L�szl� Kov�cs
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks JD.
 
Ravi Shankar R
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



Cheers,
Ravi
[ July 06, 2005: Message edited by: Ravi Shankar R ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic