• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting an integer into a string

 
Jason sigel
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Jason sigel
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jason sigel
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thanks, those were helpful tips.
Jason.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic