Java - How to Convert String to Int With Examples

How to convert a String to an Int in Java? If the String contains only numbers, then the best way to convert the String to Int is by using Integer.parseInt() or Integer.valueOf().

If the String contains both numbers and characters, then we have to use regular expressions to extract numbers from the string and then convert the resulting String it to Int.

One thing to note is that parseInt(String) returns a primitive int, whereas valueOf(String) returns an Integer() object.

Convert String to Int in Java

Using Integer.parseInt()

public class ConvertStringToInt {

    public static void main(String[] args) {
        String stringNumber = "1234";
        int number = convertStringToInt(stringNumber);
        System.out.println(number);
    }

    private static int convertStringToInt(String number) {
        return Integer.parseInt(number);
    }
}

Output:

1234

Using Integer.valueOf()

public class ConvertStringToInt {

    public static void main(String[] args) {
        String stringNumber = "1234";
        int number = convertStringToInt(stringNumber);
        System.out.println(number);
    }

    private static int convertStringToInt(String number) {
        return Integer.valueOf(number);
    }
}

Output:

1234

It is important to note that if the String contains characters and numbers such as “1234abcd” then the Integer parser throws NumberFormatException as stated in Javadoc.

Using Integer.decode()

We can also use Integer.decode(). An interesting feature of decode is that it can convert to other bases, such as base 10, base 16, etc…

public class ConvertStringToInt {

    public static void main(String[] args) {
        String stringNumber = "1234";
        int number = convertStringToInt(stringNumber);
        System.out.println(number);
    }

    private static int convertStringToInt(String number) {
        return Integer.decode(number);
    }
}

Output:

1234

Apache Commons NumberUtils Class

Last but not least, we can use Apache Commons NumberUtils class to convert String to Int in Java.

All you need to do is to have the following dependency in your pom.xml file

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>

Then, you can use:

import org.apache.commons.lang3.math.NumberUtils;

public class ConvertStringToInt {

    public static void main(String[] args) {
        String stringNumber = "1234";
        int number = convertStringToInt(stringNumber);
        System.out.println(number);
    }

    private static int convertStringToInt(String number) {
        return NumberUtils.toInt(number);
    }
}

Output:

1234