How to Convert String Type Variables into int in Java Program

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.

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

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

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

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription