Probably one of the most used operations while writing code is the conversion of a string to an integer or vice versa. But you have to be careful. If the string contains characters other than digits, you might get an exception.

There are multiple ways available to do this simple conversion in Java. Let's go through these methods one by one.

Integer.parseInt() Method

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)

Integer.valueOf() Method

The valueOf() static method also 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 is 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);

Integer.decode() Method

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

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

The Integer class methods (parseInt(), valueOf(), decode()) throw a NumberFormatException if the string is not a parsable integer. You should use 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 Next: Data Type Conversions in Java to learn about other data type conversions like string to date, string to float, string to double, and more.

✌️ Like this article? Follow @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Buy me a coffee (cost $3)

Need help to start a new Spring Boot or MEAN stack project? I am available for contract work. Hire me to accomplish your business goals with engineering and design. Let’s talk about your project: hi@attacomsian.com.