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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117902{{"`Java Long parseUnsignedLong Method`"}} java/classes_objects -.-> lab-117902{{"`Java Long parseUnsignedLong Method`"}} java/class_methods -.-> lab-117902{{"`Java Long parseUnsignedLong Method`"}} java/exceptions -.-> lab-117902{{"`Java Long parseUnsignedLong Method`"}} java/modifiers -.-> lab-117902{{"`Java Long parseUnsignedLong Method`"}} java/oop -.-> lab-117902{{"`Java Long parseUnsignedLong Method`"}} java/packages_api -.-> lab-117902{{"`Java Long parseUnsignedLong Method`"}} java/user_input -.-> lab-117902{{"`Java Long parseUnsignedLong Method`"}} java/wrapper_classes -.-> lab-117902{{"`Java Long parseUnsignedLong Method`"}} java/identifier -.-> lab-117902{{"`Java Long parseUnsignedLong Method`"}} java/arrays -.-> lab-117902{{"`Java Long parseUnsignedLong Method`"}} java/comments -.-> lab-117902{{"`Java Long parseUnsignedLong Method`"}} java/data_types -.-> lab-117902{{"`Java Long parseUnsignedLong Method`"}} java/operators -.-> lab-117902{{"`Java Long parseUnsignedLong Method`"}} java/output -.-> lab-117902{{"`Java Long parseUnsignedLong Method`"}} java/strings -.-> lab-117902{{"`Java Long parseUnsignedLong Method`"}} java/variables -.-> lab-117902{{"`Java Long parseUnsignedLong Method`"}} java/system_methods -.-> lab-117902{{"`Java Long parseUnsignedLong Method`"}} end

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