Home  >  Article  >  Java  >  Java program to convert a string to an integer

Java program to convert a string to an integer

WBOY
WBOYforward
2023-08-30 20:41:181161browse

Java program to convert a string to an integer

To convert String to int in Java we can use two built-in methods namely parseInt() and valueOf(). These static methods belong to the Integer class of the java.lang package and throw a NumberFormatException if the string is not a valid representation of an integer. In Java, String is a class in the 'java.lang' package that stores a sequence of characters enclosed in double quotes. Also, integers are primitive data types that store numerical values. In this article, we will discuss some Java programs to illustrate how to convert a given string into an integer.

Java program to convert string to int

As mentioned before, we will convert string to integer using the following method -

  • Integer.parseInt()

  • Integer.valueOf()

Let us discuss these methods one by one with the help of a sample program.

Use Integer.parseInt()

The Integer.parseInt() method takes a string as argument and parses it into a raw integer value.

grammar

Integer.parseInt(String val);

Example

The following Java program demonstrates how to use the parseInt() method to convert String to int.

method

  • Initialize a string that will be converted to an integer.

  • Next, use the getClass() and getSimpleName() methods to check and print the type of the defined string.

  • Now, use the parseInt() method to convert the string to an integer and store the result in an integer variable.

  • Finally check and print the data type to confirm whether the string is converted to an integer.

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

Use Integer.valueOf()

The Integer.valueOf() method can take a string, character, or int as a parameter, but returns an Integer object that holds the value of the parsed integer.

grammar

Integer.valueOf(String val);

Example 1

This is another Java program that will show how to convert String to 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

We mentioned earlier that the parseInt() and valueOf() methods will throw NumberFormatException if the passed string is not a valid representation of an integer. In this Java program, we will pass a string containing alphabetical characters instead of numeric characters to display 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)

in conclusion

Integer.parseInt() and Integer.valueOf() are both very useful methods for converting strings to integers. However, the Integer.valueOf() method performs significantly faster than the Integer.parseInt() method. We discuss three Java programs to illustrate the practical implementation of these methods.

The above is the detailed content of Java program to convert a string to an integer. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete