Convert Java String to int

There are several ways to convert Java String to int. Let’s explore them.

Integer.parseInt() method

We have three overloaded parseInt() methods from the Integer class that we can use to convert String to int in Java:

// Parses the string argument as a signed decimal integer.
public static int parseInt(String s) throws NumberFormatException {}

// Parses the string argument as a signed integer in the radix specified by the second argument.
public static int parseInt(String s, int radix) throws NumberFormatException {}

// parses the CharSequence argument as a signed integer in the specified radix argument, beginning at the specified beginIndex and extending to endIndex - 1
public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix) {}

Example

public class Test {

  public static void main(String[] args) {

    String numberAsString = "123";

    int num = Integer.parseInt(numberAsString);

    int num2 = Integer.parseInt(numberAsString, 16); // base 16

    System.out.println(num);
    System.out.println(num2);

  }
}
Output: 123 291
 
Both methods throw NumberFormatException if the passed string is null or empty (its length is 0).

Integer.valueOf() method

Integer.valueOf() method returns Integer class instead of a primitive type int

Example

public class Test {

  public static void main(String[] args) {

    String numberAsString = "1028";

    Integer num = Integer.valueOf(numberAsString);

    System.out.println(num);
  }
}
Output: 1028
 
There is also one more overloaded method public static Integer valueOf(String s, int radix) which returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument. Similar to parseInt(String s, int radix) method.
 
Both valueOf() methods throw NumberFormatException if the argument string is not a parsable Integer in base 10. And both methods calls parseInt() method internally.
 
Check out this tutorial Exception Handling in Java to learn how to handle such exceptions in your code.

Integer.decode() method

We can also use the decode() method to convert String to Integer class or primitive type int.

Example

public class Test {

  public static void main(String[] args) {

    String numberAsString = "528";

    Integer num = Integer.decode(numberAsString);

    int num2 = Integer.decode(numberAsString);

    System.out.println(num);
    System.out.println(num2);
  }
}
Output: 528 528

NumberUtils.toInt() method

We can also use the toInt() method from NumberUtils class that belongs to the Apache Commons library.

Example

import org.apache.commons.lang.math.NumberUtils;

public class Test {

  public static void main(String[] args) {

    String numberAsString = "728";

    Integer num = NumberUtils.toInt(numberAsString);

    System.out.println(num);
  }
}
Output: 728
 
That’s it.
 
Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *