- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to convert a String to int
To convert a String to int in Java, we can use the two built-in methods namely parseInt() and valueOf(). These static methods belong to the Integer class of java.lang package and throws a NumberFormatException if the string is not a valid representation of an integer. In Java, String is a class of 'java.lang' package that stores a series of characters enclosed within double quotes. And, an integer is a primitive datatype that stores numerical values. In this article, we will discuss a few Java programs that illustrate how to convert a given String to integer.
Java Program to Convert a String to int
As mentioned earlier, we will use the following methods to convert a string into an integer −
Integer.parseInt()
Integer.valueOf()
Let's discuss these methods one by one with the help of example programs.
Using Integer.parseInt()
The Integer.parseInt() method takes a string as a parameter and parses it into a primitive integer value.
Syntax
Integer.parseInt(String val);
Example
The following Java program demonstrates how to convert a String into int using the parseInt() method.
Approach
Initialize a string which will be converted to integer.
Next, check and print the type of defined string using the getClass() and getSimpleName() methods.
Now, convert the string into integer using parseInt() method and store the result in an integer variable.
In the end, check and print the datatype to confirm whether the string is converted into integer or not.
public class Example1 { public static void main( String args[] ) { // initializing a string String inputString = "99999"; // to check the datatype of string System.out.print("Type of inputString: "); System.out.println(inputString.getClass().getSimpleName()); // converting the string into an integer int newVal = Integer.parseInt(inputString); // printing the result System.out.println("The given String after converting into Integer: " + newVal); // to check the datatype of integer System.out.print("Type of given String after converting into Integer: "); System.out.println(((Object)newVal).getClass().getSimpleName()); } }
Output
Type of inputString: String The given String after converting into Integer: 99999 Type of given String after converting into Integer: Integer
Using Integer.valueOf()
The Integer.valueOf() method can take a string, a char or an int as a parameter but returns an Integer object that holds the value of the parsed integer.
Syntax
Integer.valueOf(String val);
Example 1
This is another Java program that will show how we can convert a String into int with the help of Integer.valueOf() method.
public class Example2 { public static void main( String args[] ) { // initializing a string String inputString = "30072023"; // to check the datatype of string System.out.print("Type of inputString: "); System.out.println(inputString.getClass().getSimpleName()); // converting the string into an integer int newVal = Integer.valueOf(inputString); // printing the result System.out.println("The given String after converting into Integer: " + newVal); // to check the datatype of integer System.out.print("Type of given String after converting into Integer: "); System.out.println(((Object)newVal).getClass().getSimpleName()); } }
Output
Type of inputString: String The given String after converting into Integer: 30072023 Type of given String after converting into Integer: Integer
Example 2
Earlier we mentioned that the parseInt() and valueOf() methods throw a NumberFormatException if the passed string is not a valid representation of an integer. In this Java program, we will pass a string with alphabetical characters instead of numerical characters to show the exception.
public class Example3 { public static void main( String args[] ) { // initializing a string String inputString = "TutorialsPoint"; // converting the string to the integer int newVal = Integer.valueOf(inputString); // will give exception // printing the result System.out.println("The given String after converting into Integer: " + newVal); } }
Output
Exception in thread "main" java.lang.NumberFormatException: For input string: "TutorialsPoint" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:665) at java.base/java.lang.Integer.valueOf(Integer.java:992) at Example3.main(Example3.java:6)
Conclusion
Both Integer.parseInt() and Integer.valueOf() are very useful methods for converting strings to integers. But, the performance of Integer.valueOf() method is significantly faster than Integer.parseInt() method. We have discussed three Java programs that illustrate the practical implementation of these methods.