A string to an integer conversion is one of the most used operations while writing code. There are multiple ways available to do this simple conversion in Java.

Let us go through these methods one by one. If you want to convert an integer to a string, read this article.

Convert a string to an integer using Integer.parseInt()

The parseInt() static method from Integer class converts a string into a primitive integer (int). It takes two arguments. The first argument is the string to convert. The second optional argument is the base number called radix:

String str = "85";
int num = Integer.parseInt(str);
System.out.println(num); // 85

// with radix 16
String strBase16 = "100";
int num2 = Integer.parseInt(strBase16, 16);
System.out.println(num2); // 256

If the string contains non-digit characters, even a dot (.), the parseInt() method throws a NumberFormatException:

String str = "19.49";
int price = Integer.parseInt(str);
System.out.println(price);

Here is the output for the above conversion:

Exception in thread "main" java.lang.NumberFormatException: For input string: "19.49"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)

Convert a string to an integer using Integer.valueOf()

The valueOf() static method converts a string into an integer. But it, unlike the parseInt() method, returns an Integer object instead of a primitive int value. The NumberFormatException is thrown if the string is not a parsable integer:

Integer num1 = Integer.valueOf("19");
System.out.println(num1); // 19

Integer num2 = Integer.valueOf("20", 16);
System.out.println(num2); // 32

Integer num3 = Integer.valueOf("49%"); // NumberFormatException
System.out.println(num3);

Convert a string to an integer using Integer.decode()

The decode() static method accepts one parameter, a string, and decodes it into an Integer object. If the string is not parsable, a NumberFormatException is thrown:

Integer num1 = Integer.decode("489");
System.out.println(num1); // 489

Integer num2 = Integer.decode("41");
System.out.println(num2); // 41

Integer num3 = Integer.decode("4MB"); // NumberFormatException
System.out.println(num3);

Convert a string to an integer using Scanner

Although not very popular, the Scanner class can also be used to convert a string into an integer. The nextInt() method scans the next token of the input as a primitive int:

System.out.println(new Scanner("124").nextInt()); // 124
System.out.println(new Scanner("189").nextInt()); // 189

NumberFormatException Exception

The Integer class methods (parseInt(), valueOf(), decode()) throw a NumberFormatException if the string is not a parsable integer. You use the try-catch block to handle this exception:

try {
    String str = "9.49";
    int price = Integer.parseInt(str);
    System.out.println(price);
} catch (NumberFormatException ex) {
    ex.printStackTrace();
}

Read this guide to learn about other data type conversions like string to date, a string to float, a string to double, and more in Java.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.