In this short and simple post we are going to explain how to get an int or long number from a String.

It is as easy as doing this:

int i = 0;
long l = 0;

String s = "100000";

try {

    // We get an int from s
    i = Integer.parseInt(s);

    // We get a long from s
    l = Long.parseLong(s);

} catch(NumberFormatException nfe) {

    System.err.println("The String " + s + " is not a valid number.");
    nfe.printStackTrace();

}