Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Turning an Int into a String

 
Adam Blais
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 198
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wrap the int as an Integer, then call toString.
 
James Carman
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.valueOf( someInt )
 
James Carman
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, you can use String.valueOf() on ANYTHING! Even a null reference (which returns the string "null").
 
Igor Stojanovic
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic