Get your CodeRanch badge!*
The moose likes Beginning Java and the fly likes Converting an integer into a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Converting an integer into a string" Watch "Converting an integer into a string" New topic
Author

Converting an integer into a string

Jason sigel
Ranch Hand

Joined: Oct 07, 2003
Posts: 30
Hey guys!!
I have written a piece of code in which i have defined a method which takes in a String object as an argument.
When i actually call the method in my code, i pass it an integer(in one of the calls). How do i convert THAT integer into a string so that my code works fine?
I know there is the toString method...but am not sure how it works.
Jason!!
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29279
    
140

Jason,
To convert an integer into string, you can just concatenate it with a string:

If you need to convert back, you use:

The toString() method is for when you have an object that you want to convert to a string. If you just have a regular int primitive, toString() can't be used. (You could create an Integer object out of the primitive and call toString() on that, but it's more work than necessary.)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jason sigel
Ranch Hand

Joined: Oct 07, 2003
Posts: 30
Thanks!!
But am still confused with the method thing that u wrote above.
Say for example, i have the metho
String getISBN(String title){
blah
blah
}
and now i call it using, say
Integer approvedIsbn = this.getISBN(approved_num);
Here i am passing it an integer namely approved number, so how do i convert it into a string before calling the getISBN method.
thanks in advance
Jason.
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
if appoved_num is type of int, use String.valueOf(approved_num)
which returns a string.
besides, return type of getISBN(String title) is String, you should not
assign it to Integer

Originally posted by Jason sigel:
Thanks!!
But am still confused with the method thing that u wrote above.
Say for example, i have the metho
String getISBN(String title){
blah
blah
}
and now i call it using, say
Integer approvedIsbn = this.getISBN(approved_num);
Here i am passing it an integer namely approved number, so how do i convert it into a string before calling the getISBN method.
thanks in advance
Jason.

[ November 30, 2003: Message edited by: chi Lin ]

not so smart guy still curious to learn new stuff every now and then
Jason sigel
Ranch Hand

Joined: Oct 07, 2003
Posts: 30
Hey thanks, those were helpful tips.
Jason.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting an integer into a string
 
Similar Threads
Boxing confusion
URLyBird GUI MVC <-> Business Layer in a Thin Client and HashMap vs. ArrayList
please help me on Generics!!!!
Mixing of code - raw and generic
JDK 1.5 AutoBoxing Confusion in Code