| Author |
converting string to integer
|
Ann-Marie Russell
Greenhorn
Joined: Sep 29, 2004
Posts: 6
|
|
I am new to Java, coming from VB.NET. In VB it is rather simple to convert from any single datatype to another by doing a ctype(). I am trying to convert a string to an integer using java. I know that i can conver types such as a float to an integer using: int blah = (int) float but i receive an error when trying to do this to a string...any words of advice are welcome. thanks, AMR
|
amr04
|
 |
David Ulicny
Ranch Hand
Joined: Aug 04, 2004
Posts: 724
|
|
int i = Integer.parseInt("1"); int j = new Integer("1").intValue();
|
SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24054
|
|
Hi Ann-Marie, Welcome to JavaRanch! The static method java.lang.Integer.parseInt() is one way to do this: Likewise, there's a parseDouble() in java.lang.Double, a parseFloat() in java.lang.Float(), etc. These methods report failure -- i.e., a String that doesn't look like a valid number -- by throwing NumberFormatException; a proper program has to catch and deal with that error.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Ramaswamy Srinivasan
Ranch Hand
Joined: Aug 31, 2004
Posts: 295
|
|
Hi Ann-Marie, I agree with this way of converting types suggested by Ernst.
String s = "35"; int i = Integer.parseInt(s);
And thanks Ernst for the insight into other parseXXXX() methods. I came across a good deal of information on Casting and datatypes in the book Head First Java - by Kathy Sierra & Bert Bates. You can get a good start, especially if u r starting with Java. Cheers, Swamy
|
 |
Chengwei Lee
Ranch Hand
Joined: Apr 02, 2004
Posts: 884
|
|
Don't forget to put your codes in a try-catch block, you never know when the string argument that you're receiving could turn out to be unparsable.
|
SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
|
 |
Ramaswamy Srinivasan
Ranch Hand
Joined: Aug 31, 2004
Posts: 295
|
|
Hi Cheng...
string argument that you're receiving could turn out to be unparsable
Thanks for pointing that out. If you don't mind, can u give some elaboration on the Unparsable Strings. Like some instances where this can happen? Cheers, Swamy
|
 |
Jeff Bosch
Ranch Hand
Joined: Jul 30, 2003
Posts: 804
|
|
|
|
Give a man a fish, he'll eat for one day. <br />Teach a man to fish, he'll drink all your beer.<br /> <br />Cheers,<br /> <br />Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
|
 |
Nigel Browne
Ranch Hand
Joined: May 15, 2001
Posts: 673
|
|
|
or maybe
|
 |
Mark Vedder
Ranch Hand
Joined: Dec 17, 2003
Posts: 624
|
|
Originally posted by Ramaswamy Srinivasan: If you don't mind, can u give some elaboration on the Unparsable Strings. Like some instances where this can happen? Originally posted by Jeff Bosch:
Or the equally as bad, but maybe less obvious: The following would work (i.e. not throw an exception)...
|
 |
Ramaswamy Srinivasan
Ranch Hand
Joined: Aug 31, 2004
Posts: 295
|
|
Hi all, Thanks a lot guys, for making the thing clear.. Cheers, Swamy
|
 |
 |
|
|
subject: converting string to integer
|
|
|