Signup/Sign In
LAST UPDATED: OCTOBER 16, 2020

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

Java parseUnsignedInt(CharSequence s, int beginText, int endText, int radix) method is a part of the Integer class of the java.lang package. This method returns the unsigned integer equivalent after the character sequence passed is parsed in accordance with the integer radix value beginning from the passed beginning index and extends to passed (ending index - 1). The method does not take steps to guard against the CharSequence being mutated while parsing.

Syntax:

public static int parseUnsignedInt(CharSequence s, int beginIndex, int endIndex, int radix)  

Parameters:

s: The character sequence to be parsed.

beginIndex: The index of the character sequence from where parsing will begin.

endIndex: The index of the character sequence until where the parsing will extend.

radix: The integer value according to which the character sequence will be passed.

Returns:

Returns the equivalent unsigned integer value obtained after parsing the character sequence in accordance with the integer radix beginning from the begin index and extends till end index-1.

Exception:

NullPointerException: This exception occurs when the input character is null.

NumberFormatException: This exception occurs when the input character sequence is not parsable.

IndexOutOfBoundsException: This exception takes place when either begin index is negative or the begin index value is greater than that of the end index value or end index value is greater than s.length().

Example 1:

Here, the unsigned integer value of the parsed CharSequence is returned.

import java.lang.Integer;

public class StudyTonight {
  public static void main(String[] args) throws NumberFormatException {
    CharSequence s1 = "332";
    CharSequence s2 = "442";
    int radix = 8;
    //returns the unsigned Integer value of the entered string in accordance with the radix and beginning and end index

    int res1 = Integer.parseUnsignedInt(s1, 0, 1, radix);
    System.out.println(res1);

    int res2 = Integer.parseUnsignedInt(s2, 0, 2, radix);
    System.out.println(res2);
  }
}


3
36

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.lang.Integer;
import java.util.Scanner;

public class StudyTonight {
  public static void main(String[] args) {
    try {
      System.out.println("Enter value");
      Scanner sc = new Scanner(System. in );

      CharSequence s1 = sc.nextLine();
      System.out.println("Enter radix");
      int radix = sc.nextInt();
      System.out.println("Enter start index");
      int si = sc.nextInt();
      System.out.println("Enter end index");
      int ei = sc.nextInt();
      //returns the unsigned Integer value of the entered string in accordance with the radix and beginning and end index

      int res1 = Integer.parseUnsignedInt(s1, si, ei, radix);
      System.out.println(res1);
    }
    catch(Exception e) {
      System.out.println("Cannot parse this value");
    }
  }
}


Enter value
501
Enter radix
8
Enter start index
0
Enter end index
2
40
********************
Enter value
-45
Enter radix
8
Enter start index
0
Enter end index
1
Cannot parse this value



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.