This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes int to String conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "int to String conversion" Watch "int to String conversion" New topic
Author

int to String conversion

Anandh Ramesh
Ranch Hand

Joined: Dec 15, 2004
Posts: 61
hi,

i have a variable of type "int". i want to convert it to a string. can anyone help me on how to do it?

any help is appreciated.


cheers,<br />Anandh
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2300
int i = 3;
String str = new Integer(i).toString();


Rahul Bhattacharjee
LinkedIn - Blog
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 8143
    
  52

Other way to do this:



[My Blog] [JavaRanch Journal]
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2300
Jaikiran Pai's way is more prefered than the way that I have mentioned.As with his code we are not creating any object ,rather using a static helper finction of String class.
Anandh Ramesh
Ranch Hand

Joined: Dec 15, 2004
Posts: 61
thanks friends
Dave Lenton
Ranch Hand

Joined: Jan 20, 2005
Posts: 1241
Originally posted by Rahul Bhattacharjee:
Jaikiran Pai's way is more prefered than the way that I have mentioned.As with his code we are not creating any object ,rather using a static helper finction of String class.


Also, if the type of i changes (for example to a long), the line of code doing the conversion will not need to change.

There's a good article on String conversion here:
http://www.yoda.arachsys.com/java/stringconv.html


There will be glitches in my transition from being a saloon bar sage to a world statesman. - Tony Banks
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 679

What if you have a huge number and you want to include the thousand separators. I believe that you can use a NumberFormatter object to do this conversion to a String, but I don't know how.

-- Kaydell
Leange Hsiung
Greenhorn

Joined: Dec 05, 2006
Posts: 3
How about the method:

int i = 1;
String str = ""+i;

Can someone explain the difficents with above?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 8143
    
  52



Dave Lenton, mentioned a link in his post earlier. Have a look at that, it answers your question.
Leange Hsiung
Greenhorn

Joined: Dec 05, 2006
Posts: 3
got it , Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: int to String conversion
 
Similar Threads
Wrapper
parse int to string???help!!!!
Change a String to an int
String to Int
class method and strings