How to Convert String to Int in Java

“How can I convert string of numbers to integer in Java”. This question looks very simple but converting a string to an int is more complicated than just converting a number. It would be best if you thought about the following issues:

  • Does the string only contain numbers 0-9 ?
  • What happens with – / + before the number? for example, how would the code deal with negative numbers?
  • What happened to MAX _- / MIN_INFINITY? What happens if the string is some really big number?

Two widespread java methods that can be used to convert string to integer are Integer.valueOf and Integer.parseInt. The usage of these two functions is slightly different.

  • valueOf returns a new or cached instance java.lang.Integer
  • parseInt returns a primitive int.

The same applies to all cases like: Short.valueOf/ parseShort, Long.valueOf/ parseLong, etc.

The Java documentation that explains about parseInt shows that this function can throw NumberFormatException, which of course, you have to handle. It is always important to handle this exception when trying to get the values ​​from command line arguments or when dynamically parsing something

int number;
try {
   number = Integer.parseInt(str);
}
catch (NumberFormatException e)
{
   number = 0;
}

You can also use decode public static Integer decode(String nm) throws NumberFormatException

Along with base 10, It works for base 8 and 16:

// base 10
Integer.decode("12");       // 12 - Integer
// base 8
Integer.decode("012");     // 10 - Integer
// base 16
Integer.decode("#12");      // 18 - Integer
Integer.decode("0x12");     // 18 - Integer
Integer.decode("0X12");     // 18 - Integer

If you want to get int instead Integer, you can use:

  1. Unpacking: int number = Integer.decode("10");
  2. intValue() : Integer.decode("10").intValue();

How to convert string to int in Java without try catch.

An alternative solution would be to use Apache Commons’ NumberUtils. The nice thing about the Apache commons is that if a string is in an invalid numeric format, it always returns 0. This way you save a try catch block.

You can write your own function on the top of Integer::parseInt and Integer::valueOf to make it more robust and usable.

private optional<Integer> tryParseInteger(String str) {
    try {
        return Optional.of(Integer.valueOf(string));
    } catch (NumberFormatException e) {
        return Optional.empty();
    }
}

While the above code handles exceptions, the code usage becomes clean and straightforward.

Using Integer.parseInt(yourString) Remember the following cases :

Integer.parseInt("2"); / / OK

Integer.parseInt("-2"); / / OK

Integer.parseInt("+2"); / / OK

Integer.parseInt(" 2"); / / exception because of leading space

Integer.parseInt("2147483648"); // exception because integer is out of range

Integer.parseInt("1.1"); / / exception because the number is not integer

Integer.parseInt(""); / / exception because there is nothing to parse.

Scroll to Top