Javatpoint Logo
Javatpoint Logo

Java Integer parseInt() Method

The parseInt() method is a method of Integer class under java.lang package. There are three different types of Java Integer parseInt () methods which can be differentiated depending on its parameter.

These are:

  1. Java Integer parseInt (String s) Method
  2. Java Integer parseInt (String s, int radix) Method
  3. a Integer parseInt(CharSequence s, int beginText, int endText, int radix)

1. Java Integer parseInt (String s) Method

This method parses the String argument as a signed decimal integer object. The characters in the string must be decimal digits, except that the first character of the string may be an ASCII minus sign '-' to indicate a negative value or an ASCII plus '+' sign to indicate a positive value. It returns the integer value which is represented by the argument in a decimal integer.

2. Java Integer parseInt (String s, int radix) Method

This method parses the String argument as a signed decimal integer object in the specified radix by the second argument. The characters in the string must be decimal digits of the specified argument 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 to be returned.

3. Java Integer parseInt (CharSequence s, int beginText, int endText, int radix)

This method parses the CharSequence argument as a signed integer in the specified radix argument, beginning at the specified beginIndex and extending to endIndex - 1. This method does not take steps to guard against the CharSequence being mutated while parsing.

Syntax:

Following are the declarations of parseInt () method:

Parameter:

DataType Parameter Description Required/Optional
String s It is a String which needs to be converted into the Integer equivalent. Required
int radix The radix to be used while parsing the String Required
int beginIndex The beginning index, inclusive. Required
int endIndex The ending index, exclusive. Required
CharSequence s It is the CharSequence which needs to be converted into the Integer equivalent. Required

Returns:

Method Returns
parseInt (String s) This method returns the integer value which is represented by the argument in decimal equivalent.
parseInt (String s, int radix) This method returns the integer value which is represented by the string argument in the specified radix.
parseInt (String s, int radix) This method returns the integer value which is represented by the string argument in the specified radix.

Exceptions:

NullPointerException: If s is null.

IndexOutOfBoundsException: If beginIndex is negative, or if beginIndex is greater than endIndex or if endIndex is greater than s.length ().

NumberFormatException: If the CharSequence does not contain a parsable int in the specified radix, or if radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.

Compatibility Version:

Java 1.2 and above:

  • Java Integer parseInt (String s)
  • Java Integer parseInt (String s, int radix)

Java 9:

  • Java Integer parseInt (CharSequence s, int beginText, int endText, int radix)

Example 1

Test it Now

Output:

Value = 20
Value = 20
Value = -20

Example 2

Test it Now

Output:

Value = 104
Value = 512
Value = -484

Example 3

Test it Now

Output:

200100
300

Example 4

Test it Now

Output:

int i = 100

Example 5

Test it Now

Output:

Exception in thread "main" java.lang.NumberFormatException: For input string: "10A"
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.base/java.lang.Integer.parseInt(Integer.java:652)
	at java.base/java.lang.Integer.parseInt(Integer.java:770)
	at myPackage.IntegerParseIntExample5.main(IntegerParseIntExample5.java:6)





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA