How to convert String to Integer in Java
In Java, we can use Integer.valueOf(String)
to convert a String
to an Integer
object; For unparsable String, it throws NumberFormatException
.
Integer.valueOf("1"); // ok
Integer.valueOf("+1"); // ok, result = 1
Integer.valueOf("-1"); // ok, result = -1
Integer.valueOf("100"); // ok
Integer.valueOf(" 1"); // NumberFormatException (contains space)
Integer.valueOf("1 "); // NumberFormatException (contains space)
Integer.valueOf("2147483648"); // NumberFormatException (Integer max 2,147,483,647)
Integer.valueOf("1.1"); // NumberFormatException (. or any symbol is not allowed)
Integer.valueOf("1-1"); // NumberFormatException (- or any symbol is not allowed)
Integer.valueOf(""); // NumberFormatException, empty
Integer.valueOf(" "); // NumberFormatException, (contains space)
Integer.valueOf(null); // NumberFormatException, null
Table of contents
- 1. Convert String to Integer
- 2. NumberFormatException
- 3. Convert String to Integer (Java 8 Optional)
- 4. Integer.parseInt(String) vs Integer.valueOf(String)
- 5. Download Source Code
- 6. References
1. Convert String to Integer
Below example uses Integer.valueOf(String)
to convert a String
"99" to an object Integer
.
package com.mkyong.string;
public class ConvertStringToInteger {
public static void main(String[] args) {
String number = "99";
// String to integer
Integer result = Integer.valueOf(number);
// 99
System.out.println(result);
}
}
Output
99
2. NumberFormatException
2.1 For unparsable String, the Integer.valueOf(String)
throws NumberFormatException
.
String number = "D99";
Integer result = Integer.valueOf(number);
Output
Exception in thread "main" java.lang.NumberFormatException: For input string: "D99"
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.valueOf(Integer.java:999)
at com.mkyong.string.ConvertStringToInteger.main(ConvertStringToInteger.java:10)
2.2 Try and catch the NumberFormatException
.
package com.mkyong.string;
public class ConvertStringToInteger {
public static void main(String[] args) {
String number = "D99";
try {
Integer result = Integer.valueOf(number);
System.out.println(result);
} catch (NumberFormatException e) {
//do something for the exception.
System.err.println("Invalid number format : " + number);
}
}
}
3. Convert String to Integer (Java 8 Optional)
Below is a Java 8 example of converting a String
to an Integer
object and returning an Optional<Integer>
.
package com.mkyong.string;
import java.util.Optional;
public class ConvertStringToIntegerJava8 {
public static void main(String[] args) {
String number = "99";
Optional<Integer> result = convertStringToInteger(number);
if (result.isPresent()) {
System.out.println(result.get());
} else {
System.err.println("Unable to convert the number : " + number);
}
}
private static Optional<Integer> convertStringToInteger(String input) {
if (input == null) return Optional.empty();
// remove spaces
input = input.trim();
if (input.isEmpty()) return Optional.empty();
try {
return Optional.of(Integer.valueOf(input));
} catch (NumberFormatException e) {
return Optional.empty();
}
}
}
Output
99
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
.
Integer.parseInt()
– returns anint
.Integer.valueOf()
– returns anInteger
.
int result1 = Integer.parseInt("100");
Integer result2 = Integer.valueOf("100");
5. Download Source Code
$ git clone https://github.com/mkyong/core-java
$ cd java-string