How to Convert String Type Variables into int in Java Program
Java Strings
- In Java, a string is a sequence of characters. It is an object of the class java.lang.String, which is defined in the Java standard library.
- Java provides many useful methods for working with strings, such as finding the length of a string, converting strings to upper or lower case, and searching for substrings within a string.
- You must be familiar with following topics to understand the correspond example Such as: Java Strings,
Java Data Types, Java ByteArrayInputStream Class. - To understand the how to Convert String Type Variables into int in Java Program, Read the Full Article
Steps to Convert String Type Variables into int in Java Program
- Here are the Steps to Convert a String into the InputStream in Java Program:, You can follow these steps:
- Declare a string variable that you want to convert to an integer.
- Use the parseInt method of the Integer class to convert the string to an integer:
- Use the valueOf method of the Integer class to convert the string to an integer:
- If the string representation of the number is not a valid integer, the parseInt method will throw a NumberFormatException.
- To handle this exception, wrap the parseInt method in a try-catch block.
- Note: If you want to convert the string to a different numeric data type such as long, float, or double, you can use the parseLong, parseFloat, or parseDouble methods of the corresponding classes (i.e., Long, Float, Double) respectively.
Java ByteArrayInputStream :
ByteArrayInputStream is a subclass of the InputStream class in Java and it is used to read data from a byte array.
By converting a String into a byte array, you can create a ByteArrayInputStream that reads from the byte array, thereby allowing you to convert a String into an InputStream.
Java Data Types (Primitive)
Java primitive data types, such as int, float, double, etc., do not have a direct way to convert a String into an InputStream. Primitive data types are not designed for working with input/output operations, such as reading from or writing to an InputStream.
Let’s look at a Java Program to Convert String Type Variables into int to perform certain operations.
Example 1: Java Program to Convert String Type Variables into int.
Run
public class Main { public static void main (String[]args) { String str = "7979"; try { int num = Integer.parseInt (str); System.out.println ("The converted integer is: " + num); } catch (NumberFormatException e) { System.out.println ("The string is not a valid integer."); } } }
Output
The converted integer is: 7979
Explanation:
A string variable str with the value “7979” is declared,
The ParseInt Method of the integer class is used to convert the string to an integer.
The result is assigned to the num variable, After that we will get the Converted Integer.
.
Example 2 :Java Program to Convert String Type Variables into int
Run
public class Main { public static void main (String[]args) { String str = "8055"; try { Integer num = Integer.valueOf (str); System.out.println ("Total youtube subscribers is : " + num); } catch (NumberFormatException e) { System.out.println ("The string is not a valid integer."); } } }
Output
Total youtube subscribers is : 8055
Explanation:
The program uses a try-catch block to handle the possible NumberFormatException that might be thrown if the string representation of the number is not a valid integer.
In the try block, the valueOf method of the Integer class is used to convert the string to an integer.
The result is assigned to the num variable, After that we will get the Converted Integer.
Finally, in the catch block, the program prints a message indicating that the string is not a valid integer, in case the NumberFormatException is thrown.
Outside the try-catch block, the program prints the converted integer using System.out.println("Total Youtube Subscribers is : " + num);.
Example 3: Java Program to Convert String Type Variables into int
Run
public class Main { public static void main (String[]args) { String str = "9874"; try { int num = Integer.parseInt (str); System.out.println ("Total active count is: " + num); } catch (NumberFormatException e) { System.out.println ("The string is not a valid integer."); } } }
Output
Total active count is: 9874
Explanation:
A string variable str with the value “9874” is declared,
The ParseInt Method of the integer class is used to convert the string to an integer.
The result is assigned to the num variable, After that we will get the Converted Integer.
we wrap the conversion code in a try-catch block to handle any NumberFormatException that might occur if the string representation of the number is not a valid integer.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment