Signup/Sign In

Java Long parseUnsignedLong (CharSequence s, int beginText, int endText, int radix) Method

Java parseUnsignedLong(CharSequence s, int beginText, int endText, int radix) method is a part of the Long class of the java.lang package. This method returns the unsigned long 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 long parseUnsignedLong(CharSequence s, int beginIndex, int endIndex, int radix)throws NumberFormatException  

Parameters:

The parameters passed are:

CharSequence s: The character sequence to be parsed.

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

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

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

Returns:

Returns the equivalent unsigned long 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 begin index value is greater than that of end index value or end index value is greater than s.length().

Example 1:

Here, the unsigned long value of the parsed Character Sequence is returned.

import java.lang.Long;

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

    int res1 = Long.parseUnsignedLong(s1, 0, 1, radix);
    System.out.println(res1);

    int res2 = Long.parseUnsignedLong(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.Long;
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 s = 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 Long value of the entered string in accordance with the radix and beginning and end index

      int res = Long.parseUnsignedLong(s, si, ei, radix);
      System.out.println(res);
    }
    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.