JRJC - How To Convert a String to an int

If you have a string such as "123", you cannot do math with it, or use it anywhere you could use an integer unless you first convert it to the Java int type.

There are many ways you can do this, but the easiest way is simply use the method Integer.parseInt(). Example:


    String s = "123";
    int i = Integer.parseInt( s );

For more information on the Integer class, view the Integer class docs.

There's also a little about the Integer class in Just Java 1.2 pages 194 through 196, and Just Java 2 (sixth edition) on pages 53 through 56.






For more information, please visit my java site.

And for a change of pace, you can visit my permaculture site.






For more information, please visit my java site.

And for a change of pace, you can visit my permaculture site.






© 2000 paul wheaton