Add a loop
Next, you will modify the program to accept input from the user until they quit the program.
Add the following code inside the main
method to keep the program running until the user quits:
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("Enter the Number (or -1 to quit) = ");
long k = sc.nextLong();
if (k == -1) {
System.out.println("Goodbye!");
break;
}
System.out.println("Actual Number is = " + k);
System.out.println("Hexadecimal representation is = " + Long.toHexString(k)); //returns the long value in hexadecimal base 16 as a string
}
Compile and run the program again in the terminal using the following command:
javac LongToHexadecimal.java && java LongToHexadecimal
You should see an output like the following:
Enter the Number (or -1 to quit) = 456
Actual Number is = 456
Hexadecimal representation is = 1c8
Enter the Number (or -1 to quit) = -999
Actual Number is = -999
Hexadecimal representation is = fffffffffffffc19
Enter the Number (or -1 to quit) = 754
Actual Number is = 754
Hexadecimal representation is = 2f2
Enter the Number (or -1 to quit) = -1
Goodbye!