Validate if string is a number

This example shows how to validate if a String is a number.
The first example checks if it’s and Integer by trying to parse the String to an int.
The second example can be used if you’re not sure if the possible number value contained in the String will exceed the maximum value for an int.
Here we instead use the Long class to parse the String to the datatype long.

The output from this code will be:

To use the Long class, just replace the Integer class and call parseLong() instead:

The output from this example will be:

See also:
Validate if a String contains only numbers

Search for more Java info on this site here:
Custom Search

Any thoughts or questions?