Java Convert String to Int Examples

Amir
Amir
Java Convert String to Int Examples

Java convert string to integer

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()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package io.devqa.tutorials;

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:

1
1234

Using Integer.valueOf()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package io.devqa.tutorials;

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:

1
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…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package io.devqa.tutorials;

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:

1
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

1
2
3
4
5
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>

Then, you can use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package io.devqa.tutorials;

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:

1
1234
See also