Java Integer parseInt() Method

Java Integer parseInt() Method | The parseInt() method of the Integer class parses the string object into an integer. Here the string object must contain integer values except for the first character which can be an ASCII character in order to indicate the sign of the integer that is ‘+/-’ indicating a positive or negative integer. Also see:- Java Integer hashCode() Method

Java Integer parseInt() Method Syntax

There are three overloaded forms of the parseInt() method:-

  • public static int parseInt(String s) throws NumberFormatException
  • public static int parseInt(String str, int radix) throws NumberFormatException
  • public static int parseInt(CharSequence str, int begin, int end, int radix) throws NumberFormatException

Parameters:-
str: the string which will be parsed into decimal.
radix: radix to be used to parse the string.
begin: specifying the start of the index, this is included.
end: specifying the end of the index, this is not included.
Return type: Integer.
Returns: The parsed string.

Exception thrown by these methods:-

  1. IndexOutOfBoundException: if the specified indexes are not in range or if the string has lesser or more characters than the specified index.
  2. NumberFormatException: if the string or the charSequence does not have the parsable integer value.

Integer.parseInt Java Example

The Integer.parseInt(String str) method takes only one string as a parameter which should be in decimal format. It parses the given string into the integer object. The string can have ASCII values at the beginning in order to indicate the sign of the value.

The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.

Example of Java Integer.parseInt(String str) Method

public class Main {
    public static void main(String[] args) {
        int num = Integer.parseInt("200");
        int num1 = Integer.parseInt("+200");
        int num2 = Integer.parseInt("-200");

        System.out.println("num = " + num);
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
    }
}

Output:-

num = 200
num1 = 200
num2 = -200

If the passed string doesn’t represent a number then the Integer.parseInt() method of the Java Integer class will throw java.lang.NumberFormatException.

int num = Integer.parseInt("Java");

Exception:-

Exception in thread “main” java.lang.NumberFormatException: For input string: “Java”

If the passed string is null then parseInt() will throw NumberFormatException (not the NullPointerException) because it cannot parse the null string.

String string = null;
int num = Integer.parseInt(string);

Exception:-

Exception in thread “main” java.lang.NumberFormatException: Cannot parse null string

Example of Java Integer parseInt(String str, int radix) Method

The Java Integer.parseInt(String str, int radix) method parses the string argument as a signed integer in the radix specified by the second argument.

The characters in the string must all be digits of the specified radix (as determined by whether java.lang.Character.digit(char, int) returns a nonnegative value), except that the first character may be an ASCII minus sign ‘-‘ to indicate a negative value or an ASCII plus sign ‘+’ to indicate a positive value. The resulting integer value is returned.

public class Main {
    public static void main(String[] args) {
        int num = Integer.parseInt("155", 10);
        int num1 = Integer.parseInt("+12", 8);
        int num2 = Integer.parseInt("-12", 16);
        int num3 = Integer.parseInt("1110", 2);

        System.out.println("num = " + num);
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
        System.out.println("num3 = " + num3);
    }
}

Output:-

num = 155
num1 = 10
num2 = -18
num3 = 14

Calling parsingInt(string, 10) i.e. passing 10 as the second argument is exactly the same as calling the parsingInt(string) method. Therefore both will return the int value of parsable string.

Method CallReturn Value
parseInt(“0”, 10)0
parseInt(“99”, 10)99
parseInt(“-99”, 10)-99
parseInt(“10”, 16)16
parseInt(“12”, 8)10
parseInt(“1011”, 2)11
parseInt(“9812”, 8)NumberFormatException

If the given string doesn’t represent a number then this method will throw java.lang.NumberFormatException.

int num = Integer.parseInt("Java", 10);

Exception:-

Exception in thread “main” java.lang.NumberFormatException: For input string: “Java”

Similarly, if the string represents a number but it is not valid in the given radix then it will throw java.lang.NumberFormatException. For example in an octal number system number can be between 0 to 7 and in binary numbers can be in between 0 or 1. If string contains non-allowed value then the parseInt(string, int) method will throw java.lang.NumberFormatException.

int num = Integer.parseInt("9812", 8);

Exception:-

Exception in thread “main” java.lang.NumberFormatException: For input string: “9812” under radix 8

int num = Integer.parseInt("123", 2);

Exception:-

Exception in thread “main” java.lang.NumberFormatException: For input string: “123” under radix 2

Integer parseInt In Java

The public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix)
throws NumberFormatException method
parses the CharSequence argument as a signed int in the specified radix, beginning at the specified beginIndex and extending to endIndex – 1. This overloaded form was introduced in the Java 9 version.

public class Main {
    public static void main(String[] args) {
        int num1 = Integer.parseInt("+2110", 0, 2, 8);
        int num2 = Integer.parseInt("-3524", 1, 2, 16);
        String string = "KnowProgram2025";
        int num3 = Integer.parseInt(string, 11, string.length(), 10);
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
        System.out.println("num3 = " + num3);
    }
}

Output:-

num1 = 2
num2 = 3
num3 = 2025

If the given beginIndex and endIndex are out of range then the Integer.parse() method throws IndexOutOfBoundsException.

String string = "KnowProgram2025";
int num3 = Integer.parseInt(string, 11, string.length() + 5, 10);

Exception:-

Exception in thread “main” java.lang.IndexOutOfBoundsException

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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