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

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String to Integer Conversion" Watch "String to Integer Conversion" New topic

String to Integer Conversion

Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
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.

SCJP 1.4 (88%)<br />SCWCD 1.4 (88%)
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618

Check out the 3rd constructor here:
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

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");

[Jess in Action][AskingGoodQuestions]
Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
thanks ernest,
Your are rite..it throwed an Exception and your code worked.Thanks.
Have you checked out Aspose?
subject: String to Integer Conversion
It's not a secret anymore!