In most of the cases, the best option to convert String to int in Java is to use the method Integer.parseInt(String):
1 2 3 4 |
int i = Integer.parseInt("1"); |
Let me explain this method below.
Integer.parseInt(String s) method:
1 2 3 4 5 6 7 8 9 |
public class StringToInt { public static void main(String[] args) { String string = "51"; int i = Integer.parseInt(string); System.out.println(i); } } |
1 2 3 |
51 |
Integer.parsentInt(String) method signature
You can find below the explanation what kind of parameters the int Integer.parseInt(String string) method takes, what it returns and what exception it can throw.
Parameters | Returns | Throws |
String string | int | NumberFormatException |
The string containing an int to be parsed | int value which is the result of parsing | The exception which is thrown when the parameter cannot be parsed to int |
Parsing negative int
Using the Integer.parseInt methods you can parse as well negative values. Look at the example below.
1 2 3 4 5 6 7 8 9 |
public class StringToIntNegative { public static void main(String[] args) { String string = "-60"; int i = Integer.parseInt(string); System.out.println(i); } } |
The result of running the above code is:
1 2 3 |
-60 |
Parsing an int with a plus sign
With the parseInt method, you can parse as well an int with the plus ‘+’ sign at the beginning.
1 2 3 4 5 6 7 8 9 |
public class StringToIntPlusCharacter { public static void main(String[] args) { String string = "+70"; int i = Integer.parseInt(string); System.out.println(i); } } |
The result of the code above is:
1 2 3 |
70 |
Parsing not an integer: NumberFormatException
When using the Integer.parseInt(String) you need to pass as a parameter to this method a String which can be parsed to an integer. Let’s try to pass a String as a parameter which is not an integer and see what will be the result.
1 2 3 4 5 6 7 8 9 |
public class StringToIntNotIntParameter { public static void main(String[] args) { String string = "5.1"; int i = Integer.parseInt(string); System.out.println(i); } } |
When you try to run the program you will get the exception:
1 2 3 4 5 6 7 |
Exception in thread "main" java.lang.NumberFormatException: For input string: "5.1" 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 stringToInt.StringToIntNotIntParameter.main(StringToIntNotIntParameter.java:6) |
The exception was thrown because the value 5.1 is not an integer.
Parsing a random String: NumberFormatException
You will get a similar error trying to use any other String which is not an integer with the method parseInt. Let’s have a look at the class StringToIntRandomStringParameter:
1 2 3 4 5 6 7 8 9 |
public class StringToIntRandomStringParameter { public static void main(String[] args) { String string = "tree"; int i = Integer.parseInt(string); System.out.println(i); } } |
The result this time is:
1 2 3 4 5 6 7 |
Exception in thread "main" java.lang.NumberFormatException: For input string: "tree" 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 stringToInt.StringToIntRandomStringParameter.main(StringToIntRandomStringParameter.java:6) |