This write-up will explain the below-listed methods for string to int conversion in java:
- What is the need for string to int conversion?
- What is Integer.parseInt() and how to use it in java.
- What is Integer.valueOf() and how to use it in java.
So, let’s begin!
What is the need for string to int conversion?
In Java, anytime we get the input from GUI(TextField/TextArea) then the input is received as a string. If the entered data is a string, that’s fine, but if the entered data is numeric then it can create some serious problems. Because whatever GUI(TextField/TextArea) receives, will be considered string-type data. So, how to tackle such a scenario.
Well! To deal with such issues java provides some built-in functions like Integer.parseInt() and Integer.ValueOf().
What is Integer.parseInt() and how to use it in java
It is a predefined static method used to convert a string to an integer.
Example1
For the clarity of concept, consider the below-given code block:
public static void main(String args[]) {
String value = "572";
int converted = Integer.parseInt(value);
System.out.println("Converted value: " +converted);
}
}
The Integer.parseInt() method will produce the following output:
The output shows an integer value.
Example2
The below code block will provide you more clarity:
public static void main(String args[]) {
String value = "72";
System.out.println("Result Before Conversion: " + (value + value));
int converted = Integer.parseInt(value);
System.out.println("Converted value: " + converted);
System.out.println("Result after Conversion: " + (converted + converted));
}
}
In this example, firstly, we applied the arithmetic addition operator on the original value, and afterward, we performed the same functionality on the converted values. Consequently, we will get the following output:
The output shows that Integer.parseInt() converted the given string into integer data type successfully.
What is Integer.valueOf() and how to use it in java
Java provides another useful method to convert a string to an integer value named Integer.valueOf().
Example3
In this example, we will utilize the Integer.valueOf() method to convert the string data into integer format in java:
public static void main(String args[]) {
String value = "12";
System.out.println("Result Before Conversion: " + (value + value));
int converted = Integer.valueOf(value);
System.out.println("Result after Conversion: " + (converted + converted));
}
}
The Integer.valueOf() method will generate the following output:
The output shows that the valueOf() method does exactly what we were looking for.
Example 4
Consider the below code snippet to understand how valueOf() deal with invalid(non-numeric) strings:
public static void main(String args[]) {
String value = "Java512";
int converted = Integer.valueOf(value);
System.out.println("Result: " + ( converted));
}
}
If we pass a non-numeric string to the valueOf() method it will throw a NumberFormatException:
It’s better to surround your code with try catch to avoid such type of exception.
Conclusion
In java, string to int conversion can be achieved using two build-in methods i.e., Integer.ParseInt() and Integer.ValueOf(). These methods take numeric strings as input and convert them into integer data types. If we pass an invalid/non-numeric string to these methods, then a Number Format Exception will occur. This write-up explained different methods to convert a string to int in java.