String to Integer in Java – parseInt()

While operating upon strings, there are times when we need to convert a number represented as a string into an integer type. The method generally used to convert String to Integer in Java is parseInt().

How to use parseInt() method in Java?
There are two variants of this method:

public static int parseInt(String s) throws NumberFormatException

     -  This function parses the string argument as a signed 
        decimal integer.
public static int parseInt(String s, int radix) throws NumberFormatException

     -  This function parses the string argument as a signed
        integer in the radix specified by the second argument.

Returns:
In simple words, both the methods convert the string into its integer equivalent. The only difference being is that of the parameter radix. The first method can be considered as an equivalent of the second method with radix = 10 (Decimal).

Parameters:

  • s – A string which needs to be converted to the integer. It can also have the first character as a minus sign ‘-‘ (‘\u002D’) or plus sign ‘+’ (‘\u002B’) to represent the sign of the number.
  • radix – The radix used while the string is being parsed.
    Note: This parameter is only specific to the second variant of the method.

Exceptions:
NumberFormatException is thrown by this method if any of the following situations occurs:

For both the variants:

  • String is null or of zero length
  • The value represented by the string is not a value of type int
  • Specifically for the parseInt(String s, int radix) variant of the function:
    • The second argument radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX
    • Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign ‘-‘ (‘\u002D’) or plus sign ‘+’ (‘\u002B’) provided that the string is longer than length 1

Examples:

parseInt("20") returns 20
parseInt("+20") returns 20
parseInt("-20") returns -20
parseInt("20", 16) returns 16, (2)*16^1 + (0)*16^0 = 32
parseInt("2147483648", 10) throws a NumberFormatException
parseInt("99", 8) throws a NumberFormatException 
                  as 9 is not an accepted digit of octal number system
parseInt("geeks", 28) throws a NumberFormatException
parseInt("geeks", 29) returns 11670324, 
                  Number system with base 29 can have digits 0-9 
                  followed by characters a,b,c... upto s
parseInt("geeksforgeeks", 29) throws a NumberFormatException as the 
                             result is not an integer.

Click the Run on IDE button and try converting different strings to integer yourself:

// Java program to demonstrate working parseInt()
public class GFG
{
    public static void main(String args[])
    {
        int decimalExample = Integer.parseInt("20");
        int signedPositiveExample = Integer.parseInt("+20");
        int signedNegativeExample = Integer.parseInt("-20");
        int radixExample = Integer.parseInt("20",16);
        int stringExample = Integer.parseInt("geeks",29);

        // Uncomment the following code to check
        // NumberFormatException

        //   String invalidArguments = "";
        //   int emptyString = Integer.parseInt(invalidArguments);
        //   int outOfRangeOfInteger = Integer.parseInt("geeksforgeeks",29);
        //   int domainOfNumberSystem = Integer.parseInt("geeks",28);

        System.out.println(decimalExample);
        System.out.println(signedPositiveExample);
        System.out.println(signedNegativeExample);
        System.out.println(radixExample);
        System.out.println(stringExample);
    }
}

Output:

20
20
-20
32
11670324

Similarly, we can convert the string to any other primitive data types:

parseLong() – parses the string to Long

parseDouble() – parses the string to Double

If we want to convert the string to integer without using parseInt(), we can use valueOf() method. It also has two variants similar to parseInt().

Difference between parseInt() and valueOf():
parseInt() parses the string and returns the primitive integer type. However, valueOf() returns an Integer object.
Note: valueOf() uses parseInt() internally to convert to integer.

// Java program to demonstrate working of valueOf()
public class GFG
{
    public static void main(String args[])
    {
        int decimalExample = Integer.valueOf("20");
        int signedPositiveExample = Integer.valueOf("+20");
        int signedNegativeExample = Integer.valueOf("-20");
        int radixExample = Integer.valueOf("20",16);
        int stringExample = Integer.valueOf("geeks",29);

        System.out.println(decimalExample);
        System.out.println(signedPositiveExample);
        System.out.println(signedNegativeExample);
        System.out.println(radixExample);
        System.out.println(stringExample);
    }
}

Output :

20
20
-20
32
11670324

Related Article:
Java.lang.Integer class and its methods

References:
Offical Java Documentation

This article is contributed by Shikhar Goel. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:







Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.