The Integer.parseInt() method in java converts an int string to an int value. We can also convert String to Integer object using Integer.valueOf() method. You can use the Integer.parseInt() function to get the original int value from Java String. The Integer.valueOf() method returns the Integer wrapper class from String instance to the int value from String.
Mathematical operations can be carried out for two int numbers. If the int is obtained as a string, convert the string to int by using the Integer.parseInt() function. If you want to convert the string to integer object, you can use Integer.valueOf() function. The integer object is used to work with other java objects.
To convert String to int, the string should contain digits. If the string contains characters other than numbers, the java.lang.NumberFormatException: for input string: exception will be thrown. For example, if the string contains alphabetical characters, the string cannot be converted into an integer. This is why the Java.lang.NumberFormatException will be thrown.
Solution 1 – Using Integer.parseInt() method
We can convert String to int value using java Integer.parseInt() method. The int string is expected to contain numbers. The method Integer.parseInt() converts the number to an int value. The example below illustrates how to convert a string to an int value using java.
The parseInt will take as an argument priceStr that contains value as 500. The parseInt will convert the string value to integer value. It returns as int value 500 that will be assigned to int price variable. The mathematical operation can be performed using price variable. If the mathematical operation is performed using the string variable priceStr, the output is not predictable.
package com.yawintutor;
public class StringToInt {
public static void main(String[] args) {
int tax = 5;
String priceStr = "500";
int price = Integer.parseInt(priceStr);
System.out.println("The price is " + price);
System.out.println("priceStr + tax = " + (priceStr + tax));
System.out.println("price + tax = " + (price + tax));
}
}
Output
The price is 500
priceStr + tax = 5005
price + tax = 505
Solution 2 – Using Integer.valueOf() method
The integer.valueOf() method converts an int string to an Integer object. The int string should have numbers. The Integer.valueOf() function converts the number to an Integer wrapper class object. The example below will show how to convert a string to an Integer object using java.
The Integer.valueOf() method takes priceStr string value “500”. The valueOf() method converts the string to an integer object. The Integer wrapper class object can be stored in Integer price variable. The price object can be used as like an java object that contains an int value 500.
package com.yawintutor;
public class StringToInt {
public static void main(String[] args) {
int tax = 5;
String priceStr = "500";
Integer price = Integer.valueOf(priceStr);
System.out.println("The price is " + price.intValue());
System.out.println("priceStr + tax = " + (priceStr + tax));
System.out.println("price + tax = " + (price + tax));
}
}
Output
The price is 500
priceStr + tax = 5005
price + tax = 505
Solution 3 – java.lang.NumberFormatException: For input string:
If the int string includes characters other than the number, the Integer.parseInt() string cannot be converted to the int value. In this case, the exception java.lang.NumberFormatException: For input string: will be thrown. The example below illustrates how to create a NumberFormatException and how to solve this NumberFormatException.
In the first program the price string contains a character “$”. The character can not be converted to a integer value. The parseInt() method will thrown java.lang.NumberFormatException: For input string: exception. The try catch block will handle the exception and returned with price value as 0.
package com.yawintutor;
public class StringToInt {
public static void main(String[] args) {
int tax = 5;
String priceStr = "$500";
int price = Integer.parseInt(priceStr);
System.out.println("The price is " + price);
System.out.println("priceStr + tax = " + (priceStr + tax));
System.out.println("price + tax = " + (price + tax));
}
}
Exception
Exception in thread "main" java.lang.NumberFormatException: For input string: "$500"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:569)
at java.lang.Integer.parseInt(Integer.java:615)
at com.yawintutor.StringToInt.main(StringToInt.java:8)
Solution
package com.yawintutor;
public class StringToInt {
public static void main(String[] args) {
int tax = 5;
String priceStr = "$500";
int price = 0;
try {
price = Integer.parseInt(priceStr);
} catch(NumberFormatException e) {
price =0;
}
System.out.println("The price is " + price);
System.out.println("priceStr + tax = " + (priceStr + tax));
System.out.println("price + tax = " + (price + tax));
}
}
Output
The price is 0
priceStr + tax = $5005
price + tax = 5
Solution 4 – Using Java 8
If the int string contains characters other than number, the exception “java.lang.NumberFormatException: For input string:” will be thrown. The exception should be caught using try catch block. To avoid the try catch, you can use java 8 code.
package com.yawintutor;
import java.util.Optional;
import com.google.common.primitives.Ints;
public class StringToInt {
public static void main(String[] args) {
int tax = 5;
String priceStr = "$500";
int price = Optional.ofNullable(priceStr)
.map(Ints::tryParse)
.orElse(0);
System.out.println("The price is " + price);
System.out.println("priceStr + tax = " + (priceStr + tax));
System.out.println("price + tax = " + (price + tax));
}
}
Output
The price is 0
priceStr + tax = $5005
price + tax = 5
The above example will use Ints.tryParse() method from the external source jar. The Ins class is available in the com.google.common.primitives.Ints package. The price string is converted using tryParse() method. if it fails to convert string to int value, the else block will be returned with int value 0.