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.






© 2000 Paul Wheaton