Java Long Unsigned String Conversion

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn about Java's toUnsignedString() method that is used to convert a long value into an unsigned decimal String object. You will learn how to use the method, what it takes as an argument and what it returns.


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/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/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117932{{"`Java Long Unsigned String Conversion`"}} java/classes_objects -.-> lab-117932{{"`Java Long Unsigned String Conversion`"}} java/class_methods -.-> lab-117932{{"`Java Long Unsigned String Conversion`"}} java/exceptions -.-> lab-117932{{"`Java Long Unsigned String Conversion`"}} java/modifiers -.-> lab-117932{{"`Java Long Unsigned String Conversion`"}} java/oop -.-> lab-117932{{"`Java Long Unsigned String Conversion`"}} java/packages_api -.-> lab-117932{{"`Java Long Unsigned String Conversion`"}} java/user_input -.-> lab-117932{{"`Java Long Unsigned String Conversion`"}} java/wrapper_classes -.-> lab-117932{{"`Java Long Unsigned String Conversion`"}} java/identifier -.-> lab-117932{{"`Java Long Unsigned String Conversion`"}} java/arrays -.-> lab-117932{{"`Java Long Unsigned String Conversion`"}} java/data_types -.-> lab-117932{{"`Java Long Unsigned String Conversion`"}} java/operators -.-> lab-117932{{"`Java Long Unsigned String Conversion`"}} java/output -.-> lab-117932{{"`Java Long Unsigned String Conversion`"}} java/strings -.-> lab-117932{{"`Java Long Unsigned String Conversion`"}} java/variables -.-> lab-117932{{"`Java Long Unsigned String Conversion`"}} java/string_methods -.-> lab-117932{{"`Java Long Unsigned String Conversion`"}} java/system_methods -.-> lab-117932{{"`Java Long Unsigned String Conversion`"}} end

Initializing a long Value

First, we will create a long value and initialize it with some data. We will use this value in later examples to convert it into an unsigned decimal string using the toUnsignedString() method.

long longValue = 3000000000L;

Converting long to Unsigned Decimal String

Now, we will use the toUnsignedString() method to convert our initialized long value into an unsigned decimal String.

String unsignedString = Long.toUnsignedString(longValue);

Printing the Result

Finally, we will print the results of our converted unsigned decimal String in the console.

System.out.println("Unsigned String value: " + unsignedString);

Here's the complete code for the steps above:

public class LongToUnsignedString {
    public static void main(String[] args) {
        long longValue = 3000000000L;
        String unsignedString = Long.toUnsignedString(longValue);
        System.out.println("Unsigned String value: " + unsignedString);
    }
}

When you run the code, you will see the following output:

Unsigned String value: 3000000000

Negative Long Value Handling

In Java, long data types can also store negative values. In this step, we will see what happens when we try to convert a negative value to an unsigned decimal String.

long longNegative = -123456789L;
String unsignedString = Long.toUnsignedString(longNegative);

Printing Result for Negative Long Value

Now that we have the unsigned decimal String value, we will try to print it in the console using the System.out.println() method.

System.out.println("Unsigned Decimal String value: " + unsignedString);

Here's the complete code:

public class LongToUnsignedString {
    public static void main(String[] args) {
        long longNegative = -123456789L;
        String unsignedString = Long.toUnsignedString(longNegative);
        System.out.println("Unsigned Decimal String value: " + unsignedString);
    }
}

The output will look like this:

Unsigned Decimal String value: 18446743950276013727

User-Defined Long Value Conversion

In this step, we will create a user-defined long value and convert it to an unsigned decimal String using the toUnsignedString() method.

To do that, we will create a scanner object to read from the console, and then store the scanned value in a long variable.

Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
long userLong = scanner.nextLong();

After that, we will convert the user-defined long value to an unsigned decimal string using the toUnsignedString() method.

String unsignedString = Long.toUnsignedString(userLong);

Finally, we will print the converted value to the console.

System.out.println("Unsigned String value: " + unsignedString);

Here's the complete code:

import java.util.Scanner;

public class LongToUnsignedString {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        long userLong = scanner.nextLong();
        String unsignedString = Long.toUnsignedString(userLong);
        System.out.println("Unsigned String value: " + unsignedString);
    }
}

When you run the code, you will be prompted to enter a number in the console. After you enter a value, the program will convert it to an unsigned decimal String value, and print it to the console.

Invalid Input Handling

In this step, we will modify the code from the previous step to handle invalid user input.

First, we will wrap the code from step 6 in a try-catch block to handle any exceptions that may occur due to invalid user input.

try {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter a number: ");
    long userLong = scanner.nextLong();
    String unsignedString = Long.toUnsignedString(userLong);
    System.out.println("Unsigned String value: " + unsignedString);
} catch (InputMismatchException exception) {
    System.out.println("Invalid input: Please enter a valid long value.");
}

The try block contains the same code from step 6 to read user input, convert it to an unsigned decimal String, and print it to the console.

The catch block catches any InputMismatchException that may occur due to invalid user input, and prints an error message to the console indicating that the input was not a valid long value.

Here's the complete code:

import java.util.InputMismatchException;
import java.util.Scanner;

public class LongToUnsignedString {
    public static void main(String[] args) {
        try {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter a number: ");
            long userLong = scanner.nextLong();
            String unsignedString = Long.toUnsignedString(userLong);
            System.out.println("Unsigned String value: " + unsignedString);
        } catch (InputMismatchException exception) {
            System.out.println("Invalid input: Please enter a valid long value.");
        }
    }
}

When you run the code and enter an invalid input, the program will print an error message to the console. If you enter a valid long value, the program will convert it to an unsigned decimal String value and print it to the console.

Summary

In this lab, you learned about Java's toUnsignedString() method used to convert a long data type to an unsigned decimal String. You also learned with examples how to use this method, what it takes as an argument, and what it returns.

You learned how to handle invalid input for user-defined long values, and how to handle negative long values and print them to the console in their unsigned decimal String representation.

Other Java Tutorials you may like