How to convert string to int in Java

Let’s learn how to convert a string to int in Java. The Java Integer class provides two methods to carry out this process. First is the Integer.parseInt() method, and the other is the Integer.valueOf() method.

Both the methods throw NumberFormatException if the String is empty or contains characters other than numbers, or the number in the String is out of the Integer range (-2147483648 to 2147483647). Therefore, you need to implement them in a try-catch block.

These methods allow the first character of the String to be a minus sign ‘-‘ or a plus sign ‘+’ to represent the sign of the number. If the number has a ‘+’ sign at the beginning, or zeroes at the beginning, they will be removed in the final result. For example, +99 will become 99, and 0009 will become 9.

To check why the exception is thrown, you can make use of the matches() method. This method will match the given String to the regular expression passed as its parameter and returns true if there is a match, else it will return false. Syntax:

String.matches(regex);

Where String is any String that is to be matched with the regular expression, and regex is the regular expression.

The regular expression pattern used to check if the String contains only numbers with an optional sign at the beginning is:

Regex: "(-|\\+)?[0-9.]+"

When the exception is thrown, if the matches() method returns true, then the exception is thrown because the value of the number is out of the Integer range. If the matches() method returns false, then the exception is thrown because the String contains non-numeric characters.

Convert String to int in Java

We will do these in multiple ways.

Using Integer.parseInt() method:

The Integer.parseInt() method of the Integer class converts the string into its equivalent integer. It returns the string as a primitive int type. Syntax:

Integer.parseInt(String);

Where String is the given String you need to convert to int.

Java program to implement the Integer.parseInt() method:

import java.util.Scanner;
public class StringtoInt { 
  public static void main(String args[]){  
    Scanner s = new Scanner(System.in); 
    String str = "";
    try {
      System.out.println("Enter a numeric String to be converted to int:");
      str = s.next();

      //Converting the above String to int using Integer.parseInt() method.
      int toInt = Integer.parseInt(str); 
      
      //Printing the value after converting the String to an integer.
      System.out.println("After converting the String to int: " + toInt);  
    }
    catch(NumberFormatException e) {
      if(str.matches("(-|\\+)?[0-9.]+")){
        //Occurs when the String contains a number outside the range of the Integer.
        System.out.println("Please enter the numeric characters in the range of Integer (-2147483648 to 2147483647).\n");
      }
      else {
        //Occurs when the String contains non-numeric characters.
        System.out.println("Please enter a string with only numeric characters.\n");
      }
      e.printStackTrace(); 
    }
  } 
}

Output:

  1. Enter a numeric String to be converted to int:
    -7788
    After converting the String to int: -7788
  2. Enter a numeric String to be converted to int:
    16548
    After converting the String to int: 16548
  3. Enter a numeric String to be converted to int:
    +394857483
    After converting the String to int: 394857483
  4. Enter a numeric String to be converted to int:
    Hello
    Please enter a string with only numeric characters.
    
    java.lang.NumberFormatException: For input string: "Hello"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:652)
    at java.lang.Integer.parseInt(Integer.java:770)
    at StringtoInt.main(StringtoInt.java:11)
  5. Enter a numeric String to be converted to int:
    21474836477889
    Please enter the numeric characters in the range of Integer (-2147483648 to 2147483647).
    
    java.lang.NumberFormatException: For input string: "21474836477889"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:652)
    at java.lang.Integer.parseInt(Integer.java:770)
    at StringtoInt.main(StringtoInt.java:11)

 

Using Integer.valueOf() method:

The Integer.valueOf() method of the Integer class converts the string to an Integer object. It returns the string as an Integer object. Syntax:

Integer.valueOf(String);

Where String is the given String you need to convert to int.

Java program to implement the Integer.valueOf() method:

import java.util.Scanner;
public class StringtoInt { 
  public static void main(String args[]){  
    Scanner s = new Scanner(System.in); 
    String str = "";
    try {
      System.out.println("Enter a numeric String to be converted to int:");
      str = s.next();
      
      //Converting the above String to int using Integer.valueOf() method.
      Integer toInt = Integer.valueOf(str); 
      
      //Printing the value after converting the String to an integer.
      System.out.println("After converting the String to int: " + toInt);  
    }
    catch(NumberFormatException e) {
      if(str.matches("(-|\\+)?[0-9.]+")){
        //Occurs when the String contains a number outside the range of the Integer.
        System.out.println("Please enter the numeric characters in the range of Integer (-2147483648 to 2147483647).");
      }
      else {
        //Occurs when the String contains non-numeric characters.
        System.out.println("Please enter a string with only numeric characters.");
      }
  
      e.printStackTrace();
    }
  } 
}

Output:

  1. Enter a numeric String to be converted to int:
    1234hello
    Please enter a string with only numeric characters.
    
    java.lang.NumberFormatException: For input string: "1234hello"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:652)
    at java.lang.Integer.valueOf(Integer.java:983)
    at StringtoInt.main(StringtoInt.java:11)
  2. Enter a numeric String to be converted to int:
    -2373734734738923
    Please enter the numeric characters in the range of Integer (-2147483648 to 2147483647).
    
    java.lang.NumberFormatException: For input string: "-2373734734738923"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:652)
    at java.lang.Integer.valueOf(Integer.java:983)
    at StringtoInt.main(StringtoInt.java:11)
  3. Enter a numeric String to be converted to int:
    -6779
    After converting the String to int: -6779
  4. Enter a numeric String to be converted to int:
    9008900
    After converting the String to int: 9008900

This is how you convert a String to int. Hope this tutorial helped. Happy coding!

You can also read, String to Integer and Integer to String conversion in Java.

Leave a Reply

Your email address will not be published. Required fields are marked *