How to convert a string to an integer in Java

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.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.