Convert a String to a Number

From Progzoo
Revision as of 12:22, 31 December 2007 by Andr3w (talk | contribs) (New page: ==String to int== <question xmlns:htm="" lang="java" className="Demo"><prog> public class Demo{ public static void main(String[] argv) { String s = "123...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

String to int

The static method Integer.parseInt will do the trick

There is a danger that the string does not represent a valid number in which case an exception will be raised.

There are plenty of similar routines such as: Float.parseFloat Long.parseLong Double.parseDouble Boolean.parseBoolean

The value "1234" is a string, you need to treat it as a number - to add 1, giving 1235.

[Font] [Default] [Show] [Resize] [History] [Profile]