Java - Convert String to int

To convert a String to int in Java, we can use:

Integer.parseInt()

int i1 = Integer.parseInt("101010101");
System.out.println(i1);  // 101010101

int i2 = Integer.parseInt("-8F", 16);
System.out.println(i2);  // -143

int i3 = Integer.parseInt("101010101", 2);
System.out.println(i3);  // 341
                    

We also can use Integer.parseUnsignedInt​(...) to make sure the integer is > 0.

Integer.valueOf()

The difference between valueOf(...) and parseInt(...) is valueOf(...) will return an Integer (wrapper class) instead of primitive int.

  • static Integer valueOf(String s)​: Returns an Integer object holding the value of the specified String.
  • static Integer valueOf​(String s, int radix): Returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument.
int i1 = Integer.valueOf("101010101");
System.out.println(i1);  // 101010101

int i2 = Integer.valueOf("-8F", 16);
System.out.println(i2);  // -143

int i3 = Integer.valueOf("101010101", 2);
System.out.println(i3);  // 341
                    

As you can see, implicit casting automatically unbox Integer into int.

Deprecated: Integer‘s Constructor

Integer(String s)​ is deprecated.

It is rarely appropriate to use this constructor. Use parseInt(String) to convert a string to a int primitive, or use valueOf(String) to convert a string to an Integer object.

int i1 = new Integer("888");
System.out.println(i1);  // 888

int i2 = new Integer("-0x8F");  // NumberFormatException
System.out.println(i2);
                    

Integer.decode()

static Integer decode​(String nm): Decodes a String into an Integer.

int i1 = Integer.decode("888");
System.out.println(i1);  // 888

int i2 = Integer.decode("-0x8F");  // hex
System.out.println(i2);  // -143

int i3 = Integer.decode("#8F");  // hex
System.out.println(i3);  // 143

int i4 = Integer.decode("075");  // octal
System.out.println(i4);  // 61
                    

DecimalFormat.parse()

parse​(String source) throws ParseException: Parses text from the beginning of the given string to produce a number. The method may not use the entire text of the given string.

import java.text.DecimalFormat; import java.text.ParseException;

DecimalFormat decimalFormat = new DecimalFormat();
try {
    int i = decimalFormat.parse("51234").intValue();
    System.out.println(i);  // 51234
} catch (ParseException e) {
    System.out.println(e.getMessage());
}
                    

Apache Commons NumberUtils

If using maven, you need to add commons-lang3 as your dependency.

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.8.1</version> </dependency>

and import:

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

Methods:

int i1 = NumberUtils.toInt("888");
System.out.println(i1);  // 888

int i2 = NumberUtils.toInt(null);
System.out.println(i2);  // 0

int i3 = NumberUtils.toInt("888", 0);
System.out.println(i3);  // 888

int i4 = NumberUtils.toInt("xyz", 99);
System.out.println(i4);  // 99

int i5 = NumberUtils.toInt("", 99);
System.out.println(i5);  // 99

int i6 = NumberUtils.createInteger("888");
System.out.println(i6);  // 888

int i7 = NumberUtils.createInteger("-0x8F");
System.out.println(i7);  // -143
                    

Spring NumberUtils

Make sure spring-core is added as your dependency:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.2.RELEASE</version> </dependency>

and import:

import org.springframework.util.NumberUtils;

Methods:

int i1 = NumberUtils.parseNumber("888", Integer.class);
System.out.println(i1); // 888

int i2 = NumberUtils.parseNumber("-0x8F", Integer.class);
System.out.println(i2);  // -143
                    

parseNumber also built for another Number type, not only int/Integer.

Google Guava Ints.tryParse()

The conversion also can be done using Guava’s Ints.tryParse(). Add guava as one of our dependency:

<!-- https://mvnrepository.com/artifact/com.google.guava/guava --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>27.1-jre</version> </dependency>

then import:

import com.google.common.primitives.Ints;

Methods:

int i1 = Ints.tryParse("101010101");
System.out.println(i1);  // 101010101

int i2 = Ints.tryParse("-8F", 16);
System.out.println(i2);  // -143

int i3 = Ints.tryParse("101010101", 2);
System.out.println(i3);  // 341
                    

NumberFormatException

If the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format (or null/empty) , a NumberFormatException will be thrown.

int i = Integer.parseInt("-0x8F");
System.out.println(i);  // NumberFormatException
                    

Exception in thread "main" java.lang.NumberFormatException: For input string: "-0x8F" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at com.dariawan.string.StringToInt.main(StringToInt.java:66)

My Take

My take is, I'm using Apache Commons NumberUtils.toInt(...). It's able to handle NumberFormatException, and returning default value (or 0)