How to convert a String to an int in java?
The Java Integer wrapper class provides a method parseInt
to convert a String
to an int
. Here is the method signature:
public static int parseInt(String s) throws NumberFormatException
One thing that you will note in the declaration is that the method also throws NumberFormatException
. Just note it for now, we will see below why this exception is thrown and how to handle it. Let us start with a very simple example of converting a String to int.
Simple Example
Let us forget about the exception for now and write a program that can do a conversion for us.
package com.programtalk.beginner.tutorial; public class ConvertString2Integer { public static void main(String[] args) { String str = "1234"; int intVal = Integer.parseInt(str); System.out.println(intVal); // prints 1234 } }
If you have already executed the above program, try changing the str
value to "test"
and see what happens. You would see that the program no longer prints anything but throws an exception
Exception in thread "main" java.lang.NumberFormatException: For input string: "test" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at com.programtalk.beginner.tutorial.ConvertString2Integer.main(ConvertString2Integer.java:5)
The stacktrace shows that the method has thrown NumberFormatException
as we passed in a String "test"
that can’t be converted to an int
data type.
String to int example with exception handling
Now we will create a java class that will handle NumberFormatException
in case it occurs. Additional steps added in this example
- Handle the
null String. If null
is passed toparseInt,
it will throwNullPointerException
NumberFormatException
is caught and a message is printed. If a string that is not a number like “test” is passed toparseInt
thenNumberFormatException
will be thrown-
trim()
is used to handle a space at the end or before theString
like" 23 "
will be trimmed to"23"
and converted toint 23.
If we don’t trim the String ” 23 “,parseInt
will throwNumberFormatException
package com.programtalk.beginner.tutorial; public class ConvertString2Integer { public static void main(String[] args) { // passing a null will show the message "Cannot convert null to int" convertStringToInt(null); // this would convert 23 to int convertStringToInt("23"); // In this case the trim() will make sure that the spaces are removed convertStringToInt(" 23 "); // this would throw exception convertStringToInt("test"); } private static void convertStringToInt(String str) { // handle the null value if(str == null){ System.out.println("Cannot convert null to int"); return; } try { // convert to int and also trim the string int intVal = Integer.parseInt(str.trim()); System.out.println(intVal); // prints 20 } catch (NumberFormatException ex) { System.out.println("NumberFormatException while conversion: " + ex.getMessage()); } } }
Output
Cannot convert null to int 23 23 NumberFormatException while conversion: For input string: "test"
Additional points
Convert String to Integer Wrapper class
If you want to convert s String to the Integer wrapper class that can be done by using valueOf() method of Integer class.
Integer intVal = Integer.valueOf("23");
Summary
So in this tutorial we saw how to convert String to int. I hope that has helped you. Just drop a note in the below comment section in case of questions or comments.
Nicely Explained. Thanks. Helped me with the interview
thx