Given a String str containing digits as characters, the task is to convert this given string to an integer in Java. In this article, we will learn how to convert a String to an Int 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
Methods to convert a String to an Int in Java
There are certain methods to convert a String to an Int in Java as mentioned below:
1. Use Integer.parseInt() methodÂ
This is the most simple method to convert a String to an integer. The Integer.parseInt() 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:Â
Java
// Java program to convert String to int// using Integer.parseInt() methodimport 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);    }} |
String = 1234 Integer value = 1234 String = 123s Invalid String Integer value = 0
2. Java valueOf() method
It is a method that converts String into Integer Object.Â
Below is the Implementation of the above method:
Java
// Java program to convert String to int// using Ints::tryParse methodimport java.io.*;import java.util.*;Â
// Driver Classclass GFG {    // Driver code    public static void main(String[] args)    {        String str = "1234";Â
        int val = Integer.valueOf(str);        System.out.println("Integer value = " + val);        System.out.println();    }} |
Integer value = 1234
Note: String literal, calling Integer.parseInt() or Integer.valueOf() methods throw NumberFormatException if you don’t have numbers.Â
3. Use the Ints::tryParse method of the Guava libraryÂ
Another method to convert a String to an integer is to use the Ints::tryParse method of the 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:Â
Java
// Java program to convert String to int// using Ints::tryParse methodimport com.google.common.primitives.Ints;import java.io.*;import java.util.*;Â
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);    }} |
Output
String = 1234 Integer value = 1234 String = 123s Integer value = 0
