Table of Contents
In this article, we are going to see how we can convert from a String data type into integer data type in Java.
Conversion Modes
There are two ways in which String data can be converted into integer. They are:
- Using the static method parseInt(String) of the java.lang.Integer wrapper class
- Using the static method valueOf(String) of the java.lang.Integer wrapper class
- Using the constructor of Integer wrapper class that takes String as its parameter
Let us see both the modes and how to do them.
parseInt(String) method
The class java.lang.Integer has a static method called parseInt(String) that allows the programmer to easily convert a String containing integer type data(i.e. numbers with no decimal points or post decimal values) into the corresponding Integer value. The function returns a value of primitive ‘int’ type.
Let us see a code snippet to illustrate the same:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.arpit.java2blog.java8; public class ConvertStringToInteger { public static void main(String[] args) { String string = "45"; Integer stringtoInteger = Integer.parseInt(string); int stringInt=stringtoInteger.intValue(); System.out.println(stringInt); } } |
The above code on execution provides the following output:
What if String is not convertible to int
If String is not convertible to int, you will get below exception.
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.valueOf(Integer.java:983)
at org.arpit.java2blog.java8.ConvertStringToInteger.main(ConvertStringToInteger.java:8)
Using valueOf(String) method
You can also use valueOf(String) method to convert String to int in java. You can call Integer’s intValue() to convert it to primitive type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.arpit.java2blog.java8; public class ConvertStringToInteger { public static void main(String[] args) { String string = "45"; Integer stringToInteger = Integer.valueOf(string); int stringInt=stringToInteger.intValue(); System.out.println(stringInt); } } |
The above code on execution provides the following output:
Using the Integer class constructor
The other method to convert to Integer is by using the parameterized constructor of java.lang.Integer class that accepts a String as a parameter input. This creates an object of the Integer class that stores the value of the String data sent as Integer, and can be accessed using its intValue() function or by accessing the wrapper class object itself.
Let us see the code snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.arpit.java2blog.java8; public class ConvertStringToInteger { public static void main(String[] args) { String string = "45"; Integer stringToInteger = new Integer(string); int stringInt=stringToInteger.intValue(); System.out.println(stringInt); } } |
The above code on execution provides the following output:
That’s all about String to Integer conversion.