| Author |
Turning an Int into a String
|
Adam Blais
Greenhorn
Joined: Dec 02, 2004
Posts: 27
|
|
I'm using a method with prototype InputJTextField(String, int) The string is supposed to be a number gotten from a method called getYear() that returns an int value. How do I take this int value and turn it into a String so that I can use it in InputJTextField?
|
 |
Hentay Duke
Ranch Hand
Joined: Oct 27, 2004
Posts: 198
|
|
|
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
|
Wrap the int as an Integer, then call toString.
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
James Carman
Ranch Hand
Joined: Feb 20, 2001
Posts: 580
|
|
|
String.valueOf( someInt )
|
James Carman, President<br />Carman Consulting, Inc.
|
 |
James Carman
Ranch Hand
Joined: Feb 20, 2001
Posts: 580
|
|
|
Actually, you can use String.valueOf() on ANYTHING! Even a null reference (which returns the string "null").
|
 |
Igor Stojanovic
Ranch Hand
Joined: Feb 18, 2005
Posts: 58
|
|
Question? (didnt want to start another topic since its connected with this one ) I read about autobox recently and i wonder now do we still have to wrapp primitive variables or autoboxing will do all our job so we can forget about it now ?
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Originally posted by Igor Stojanovic: ... I read about autobox recently and i wonder now do we still have to wrapp primitive variables or autoboxing will do all our job so we can forget about it now?
You can use autoboxing as long as it's clear to the compiler that a wrapped instance is required from the primitive. For example, instead of... Integer in = new Integer(5); ...you can simply use... Integer in = 5; Of course, you still can't dereference an int... String s = 5.toString(); //ILLEGAL ...but you can do this... String s = ((Integer)5).toString(); [ February 18, 2005: Message edited by: marc weber ]
|
 |
 |
|
|
subject: Turning an Int into a String
|
|
|