How to convert a String to an Int in Java?

Given String str containing digits as characters, the task is to convert this given string to integer in Java.

Examples:

Input: str = "1234"
Output: 1234

Input: str = "213s"
Output: 0
Since the String contains other than digits,
hence the integer value will be 0
  • Method 1: Use Integer.parseInt() method
    This is the most simple method to convert String to integer. This function parses the string argument as a signed decimal integer.



    Syntax:

    public static int parseInt(String s)
                throws NumberFormatException
    

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to convert String to int
    // using Integer.parseInt() method
      
    import java.io.*;
      
    class GFG {
      
        // Function to convert String to integer
        public static int convert(String str)
        {
            int val = 0;
            System.out.println("String = " + str);
      
            // Convert the String
            try {
                val = Integer.parseInt(str);
            }
            catch (NumberFormatException e) {
      
                // This is thrown when the String
                // contains characters other than digits
                System.out.println("Invalid String");
            }
            return val;
        }
      
        // Driver code
        public static void main(String[] args)
        {
      
            String str = "1234";
            int val = convert(str);
            System.out.println("Integer value = " + val);
            System.out.println();
      
            str = "123s";
            val = convert(str);
            System.out.println("Integer value = " + val);
        }
    }

    chevron_right

    
    

    Output:

    String = 1234
    Integer value = 1234
    
    String = 123s
    Invalid String
    Integer value = 0
    
  • Method 2: Use Ints::tryParse method of Guava library
    Another method to convert String to integer is to use Ints::tryParse method of Guava library. It is similar to the Integer.parseInt() method, but this method is more concise and powerful.

    Syntax:

    public static Integer tryParse(String s)
    

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to convert String to int
    // using Ints::tryParse method
      
    import java.io.*;
    import java.util.*;
    import com.google.common.primitives.Ints;
      
    class GFG {
      
        // Function to convert String to integer
        public static int convert(String str)
        {
            int val = 0;
            System.out.println("String = " + str);
      
            // Convert the String
            val = Optional.ofNullable(str)
                      .map(Ints::tryParse)
                      .orElse(0);
      
            return val;
        }
      
        // Driver code
        public static void main(String[] args)
        {
      
            String str = "1234";
            int val = convert(str);
            System.out.println("Integer value = " + val);
            System.out.println();
      
            str = "123s";
            val = convert(str);
            System.out.println("Integer value = " + val);
        }
    }

    chevron_right

    
    

    Output:

    String = 1234
    Integer value = 1234
    
    String = 123s
    Integer value = 0
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.