wood burning stoves*
The moose likes Beginning Java and the fly likes int to String conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 2308
int i = 3;
String str = new Integer(i).toString();


Rahul Bhattacharjee
LinkedIn - Blog
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10069
    
163

Other way to do this:



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

Joined: Nov 29, 2005
Posts: 2308
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: 689

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: 10069
    
163



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