Java Long parseUnsignedLong Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Java parseUnsignedLong() method. The parseUnsignedLong() method is used to parse a character sequence in accordance with the integer radix value, beginning from the passed beginning index and extending to the passed (ending index - 1). This method returns the unsigned long equivalent of the parsed character sequence.

Create Java File

Create a Java file ParseUnsignedLongExample.java in the ~/project directory using the following command:

touch ~/project/ParseUnsignedLongExample.java

Import Required Packages

Import the necessary packages required for this example.

import java.lang.Long;
import java.util.Scanner;

Create Main Class

Create a main class and the main() method.

public class ParseUnsignedLongExample {

    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");

            // Get an integer value from the user input
            int ei = sc.nextInt();

            // Call the parseUnsignedLong() method to convert the input character sequence to an unsigned long value
            long res = Long.parseUnsignedLong(s, si, ei, radix);
            System.out.println("Unsigned Value: "+res);
        } catch (Exception e) {
            System.out.println("Cannot parse this value");
        }
    }
}

Compile the Java Class

Compile the ParseUnsignedLongExample.java class using the following command:

javac ~/project/ParseUnsignedLongExample.java

Run the Java Program

Run the compiled Java program using the following command:

java ParseUnsignedLongExample

You will be asked to enter the value, radix, start index, and end index as inputs. After you enter these values, the program will call the parseUnsignedLong() method, which will convert the given character sequence to an unsigned long value and print the output.

Examples

Here are some examples.

Example 1:

Suppose you want to parse a character sequence "332" in radix 8. To extract the unsigned long value from this character sequence, start index is 0 and end index is 1. Here is the example code:

CharSequence s1 = "332";
int beginIndex = 0;
int endIndex = 1;
int radix = 8;

long res1 = Long.parseUnsignedLong(s1, beginIndex, endIndex, radix);
System.out.println("Unsigned Value: "+res1);

Output:

Unsigned Value: 3

Example 2:

Suppose you want to parse a character sequence "ABCDEF" in radix 16. To extract the unsigned long value from this character sequence, start index is 1 and end index is 6. Here is the example code:

CharSequence s1 = "ABCDEF";
int beginIndex = 1;
int endIndex = 6;
int radix = 16;

long res1 = Long.parseUnsignedLong(s1, beginIndex, endIndex, radix);
System.out.println("Unsigned Value: "+res1);

Output:

Unsigned Value: 11259375

Summary

In this lab, you learned how to use the parseUnsignedLong() method in Java to parse a character sequence in accordance with the integer radix value. You also learned how to extract the unsigned long value from the parsed character sequence using the begin index and the end index.

Other Java Tutorials you may like