Javatpoint Logo
Javatpoint Logo

Java Integer parseUnsignedInt() Method

The parseUnsignedInt() is a method of Java Integer class. There are three different types of parseUnsignedInt() method which can be differentiated depending on its parameter.

These are:

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

1. Java Integer parseUnsignedInt (String s) Method

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

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

This method parses the String argument as an unsigned 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 plus sign '+' to indicate a positive value. The resulting integer value is to be returned.

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

This method parses the CharSequence argument as an unsigned decimal 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 parseUnsignedInt() method:

Parameter:

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

Returns:

Method Returns
parseUnsignedInt(String s) This method returns the unsigned integer value which is represented by the argument in decimal.
parseUnsignedInt(String s, int radix) This method returns the unsigned integer value which is represented by the string argument in the specified radix.
parseUnsignedInt(CharSequence s, int beginText, int endText, int radix) This method returns the unsigned int value which is represented by the subsequence 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 parseUnsignedInt (String s)
  • Java Integer parseUnsignedInt (String s, int radix)

Java 9:

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

Example 1

Test it Now

Output:

Value = 20

Example 2

Test it Now

Output:

Exception in thread "main" java.lang.NumberFormatException: For input string: "ABCD"
	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.parseUnsignedInt(Integer.java:832)
	at java.base/java.lang.Integer.parseUnsignedInt(Integer.java:928)
	at myPackage.IntegerParseUnsignedIntExample2.main(IntegerParseUnsignedIntExample2.java:5

Example 3

Test it Now

Output:

Output Value = 104

Example 4

Output:

Enter the Integer Inputs: 150
Enter the Radix Value: 16
Output Value = 336

Example 5

Test it Now

Output:

Output Value = 13

Example 6

Output:

Enter the Integer Inputs: 550
Enter the Begining Index: 0
Enter the Ending Index: 2
Enter the Radix Value: 8
Output Value = 45

Output 2 with Exception:

Enter the Integer Inputs: 550
Enter the Begining Index: 2
Enter the Ending Index: 5
Enter the Radix Value: 8
Exception in thread "main" java.lang.IndexOutOfBoundsException
	at java.base/java.lang.Integer.parseUnsignedInt(Integer.java:881)
	at myPackage.IntegerParseUnsignedIntRadixExample6.main(IntegerParseUnsignedIntRadixExample6.java:15)





Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA