This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
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: 24195

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 throwed an Exception and your code worked.Thanks.
I agree. Here's the link:
subject: String to Integer Conversion
It's not a secret anymore!