In this article, we will take a look at 3 ways to convert a string to an int in java with examples code and its explanation.
Method 1 : Using parseInt() methodjava.lang.Integer class has a static parseInt()method which takes a String as an argument and returns a java.lang.Integer object.
The integer returned has a numeric value equal to the number represented by the String.
public class StringToIntConverter {
public static void main(String[] args) {
String str = "100";
int i = Integer.parseInt(str);
System.out.println("Integer value is : " + i);
}
}
This method throws a NumberFormatException, if the number in string format is not parseable into an int value, such as “abc”.
Method 2 : Using valueOf() method
This is another method of string to int conversion in java. This method converts a string to int without using parseInt() method.java.lang.Integerclass has a static valueOf()method which takes a string as an argument and returns a java.lang.Integerobject.
The integer returned has a numeric value equal to the number represented by the String.
public class StringToIntConverter {
public static void main(String[] args) {
String str = "100";
int i = Integer.valueOf(str);
System.out.println("Integer value is : " + i);
}
}
This method throws a NumberFormatException, if the number in string format is not parseable into an int value. For example, “ten”.
Method 3 : Using Integer constructorjava.lang.Integerclass has a constructor which takes a string as an argument and returns a java.lang.Integerobject.
The integer returned has a numeric value equal to the number represented by the String.
This method also converts a string to integer without using parseInt() method.
public class StringToIntConverter {
public static void main(String[] args) {
String s = "100";
int i = new Integer(str);
System.out.println("Integer value is : " + i);
}
}
Using Integer constructor has been deprecated since Java 9. Instead, valueOf() method should be used to convert a string to int.
Why convert string to integer
Suppose you have a web application which takes numeric items such as age, zip code, number of employees etc. as input from the user and store it in a database.
In ideal applications, database columns for such type of values would be of integer type and the values received from browser on the server would be in string format.
Thus a conversion from string to integer is necessary.
Let’s tweak in
parseInt()method ofjava.lang.Integerclass throws ajava.lang.NumberFormatExceptionif the String provided to it is not in number format.
For Example, if the String is 100ab, thenparseInt()will throw following errorException in thread “main” java.lang.NumberFormatException: For input string: “100ab”
- Passing
nulltoparseInt()method also throws ajava.lang.NumberFormatException. java.lang.Integercan be assigned to an object of typejava.lang.Integeror a primitive of typeintas shown in above example.
This is handled automatically by java and is called unboxing (conversion of a class to its corresponding primitive type).valueOf()method also throws ajava.lang.NumberFormatExceptionif the String passed to it is not in a numeric format.valueOf()method internally callsparseInt()method.
Hope the article was useful.