Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

converting string to integer

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i = Integer.parseInt("1");
int j = new Integer("1").intValue();
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ramaswamy Srinivasan
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or maybe
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Thanks a lot guys, for making the thing clear..

Cheers,
Swamy
 
reply
    Bookmark Topic Watch Topic
  • New Topic