• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String to Integer Conversion

 
Jas Oberai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Friends,
Can you just tell me how to change a string like this to an Integer.I know this is really stupid quetion..but any help would be appreciated.
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Check out the 3rd constructor here:
http://java.sun.com/j2se/1.3/docs/api/java/lang/Integer.html
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But of course, new Integer("2.00"), just like Integer.parseInt("2.00"), throws an exception, because 2.00 is a double, not an int.

If you have a String which may contain an arbitrary number, I think your best bet would be something like

int num = (int) Double.parseDouble("2.00");
 
Jas Oberai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ernest,
Your are rite..it throwed an Exception and your code worked.Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic