How to convert String to int in Java
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
Table of contents
- 1. Convert String to int
- 2.How to handle NumberFormatException
- 3. Integer.MAX_VALUE = 2147483647
- 4. Integer.parseInt(String) vs Integer.valueOf(String)
- 5. Download Source Code
- 6. References
1. Convert String to int
Below example uses Integer.parseInt(String)
to convert a String
"1" to a primitive type int
.
package com.mkyong.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.mkyong.string.ConvertStringToInt.main(ConvertStringToInt.java:18)
2.2 The best practice is catch the NumberFormatException
during the conversion.
package com.mkyong.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
.
package com.mkyong.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 (NumberFormatException 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");
5. Download Source Code
$ git clone https://github.com/mkyong/core-java
$ cd java-string
I am making a Movie Ticket System. User inputs seat number as “D13”. How to separate char row = ‘D’ and int column = 13?
Cut the first char.
You could use substring to separate your row from the column
Hello sir
this is well explained java String conversion.
really beautiful
thanks
if the string has trailing zeroes then the zeroes are getting truncated in java 8 , can you help ? I need to convert string 100100 to int 100100
tanks
Thanks for your information. it’s so helpfull
Thanks for this explanation.
what if I want 1 and 0 separately.
If my string has more than 10 digits,the compiler gives me an error,there’s a way to fix it? How can I read more than that from a string?
max 32 bit integer is 2,147,483,647 which is why it gives you an error you cant assign such a big number, you should assign it to 64 bit int, or leave it as a String
Use type long
how can i assign it into 64 bit
Dear Sir MKYong,
Thank you for your simple but precise explanation.
Regards.
thanks you
why don’t use apache NumberUtils, or charge the String matchs d+,I think use Integer.valueof(String) directly is not good,we can avoid the exception