In Java, we can use Integer.parseInt(String) to convert a String to an int; For unparsable String, it throws NumberFormatException.
Integer.parseInt("1"); // ok
Integer.parseInt("+1"); // ok, result = 1
Integer.parseInt("-1"); // ok, result = -1
Integer.parseInt("100"); // ok
Integer.parseInt(" 1"); // NumberFormatException (contains space)
Integer.parseInt("1 "); // NumberFormatException (contains space)
Integer.parseInt("2147483648"); // NumberFormatException (Integer max 2,147,483,647)
Integer.parseInt("1.1"); // NumberFormatException (. or any symbol is not allowed)
Integer.parseInt("1-1"); // NumberFormatException (- or any symbol is not allowed)
Integer.parseInt(""); // NumberFormatException, empty
Integer.parseInt(" "); // NumberFormatException, (contains space)
Integer.parseInt(null); // NumberFormatException, null
1. Convert String to int
Below example uses Integer.parseInt(String) to convert a String “1” to a primitive type int.
ConvertStringToInt.java
package com.favtuts.string;
public class ConvertStringToInt {
public static void main(String[] args) {
String number = "1";
// String to int
int result = Integer.parseInt(number);
// 1
System.out.println(result);
}
}
Output
1
2.How to handle NumberFormatException
2.1 For invalid number or unparsable String, the Integer.parseInt(String) throws NumberFormatException.
String number = "1A";
int result = Integer.parseInt(number); // throws NumberFormatException
Output
Exception in thread "main" java.lang.NumberFormatException: For input string: "1A"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:668)
at java.base/java.lang.Integer.parseInt(Integer.java:786)
at com.favtuts.string.ConvertStringToInt.main(ConvertStringToInt.java:18)
2.2 The best practice is catch the NumberFormatException during the conversion.
ConvertStringToInt.java
package com.favtuts.string;
public class ConvertStringToInt {
public static void main(String[] args) {
String number = "1A";
try {
int result = Integer.parseInt(number);
System.out.println(result);
} catch (NumberFormatException e) {
//do something for the exception.
System.err.println("Invalid number format : " + number);
}
}
}
Output
Invalid number format : 1A
3. Integer.MAX_VALUE = 2147483647
Be careful, do not convert any numbers greater than 2,147,483,647, which is Integer.MAX_VALUE; Otherwise, it will throw NumberFormatException.
ConvertStringToInt2.java
package com.favtuts.string;
public class ConvertStringToInt2 {
public static void main(String[] args) {
// 2147483647
System.out.println(Integer.MAX_VALUE);
String number = "2147483648";
try {
int result = Integer.parseInt(number);
System.out.println(result);
} catch (Exception e) {
//do something for the exception.
System.err.println("Invalid number format : " + number);
}
}
}
Output
2147483647
Invalid number format : 2147483648
4. Integer.parseInt(String) vs Integer.valueOf(String)
The Integer.parseInt(String) convert a String to primitive type int; The Integer.valueOf(String) convert a String to a Integer object. For unparsable String, both methods throw NumberFormatException.
int result1 = Integer.parseInt("1");
Integer result2 = Integer.valueOf("1");
Download Source Code
$ git clone https://github.com/favtuts/java-core-tutorials-examples
$ cd java-string