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.
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.
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;
Now, we will use the toUnsignedString()
method to convert our initialized long value into an unsigned decimal String.
String unsignedString = Long.toUnsignedString(longValue);
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
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);
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
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.
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.
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.