Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to convert integer to string and string to integer?  RSS feed

 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know a method to convert string to interger:
for example:
String strNum = "123";
int intNum = Integer.parseInt(strNum);
Is there are any other way to do the same thing?? By the way, how to use toString(); when should I include argument in the () and don't.
Mindy Wu
 
Art Metzer
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Mindy.
If you decide to go with your code, remember that parseInt() throws a NumberFormatException.
You could also use

To convert an int to a String, use any one of the following:

(This information from the Java FAQ.)
And, to my knowledge, the toString() method never takes any arguments. To use it, just call it from whatever object you'd like to see in string form: Integer.toString(), Object.toString(), etc. The resultant object will of course be a String.
Hope this helps, Mindy.
Art
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to look up the intValue() method. To convert in int to a string object, You can just wrap your in value using the Integer() method.
Bosun
 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all! That's really help me out!

Happy programmer!
Mindy
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
Thoughts on deprecation in Java
https://coderanch.com/t/683016/java/Deprecation-Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!