Creating an Interactive Program with Scanner
Now, let's create an interactive program that lets the user input two long values and then compares them using the Long.compare()
method.
For this, we'll use the Scanner
class, which allows us to read input from the user.
- Update the
LongCompare.java
file with the following code:
import java.util.Scanner;
public class LongCompare {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Long Compare Tool!");
System.out.println("This program compares two long values that you enter.");
System.out.println("----------------------------------------");
// Prompt the user to enter the first number
System.out.print("Enter the first long number: ");
long firstNumber;
// Use a try-catch block to handle invalid input
try {
firstNumber = scanner.nextLong();
} catch (Exception e) {
System.out.println("Invalid input. Please enter a valid long number.");
return;
}
// Prompt the user to enter the second number
System.out.print("Enter the second long number: ");
long secondNumber;
// Use a try-catch block to handle invalid input
try {
secondNumber = scanner.nextLong();
} catch (Exception e) {
System.out.println("Invalid input. Please enter a valid long number.");
return;
}
// Compare the two numbers
int result = Long.compare(firstNumber, secondNumber);
// Display the result
System.out.println("\nResult of comparing " + firstNumber + " and " + secondNumber + ":");
if (result > 0) {
System.out.println(firstNumber + " is greater than " + secondNumber);
} else if (result < 0) {
System.out.println(firstNumber + " is less than " + secondNumber);
} else {
System.out.println(firstNumber + " is equal to " + secondNumber);
}
// Close the Scanner to release resources
scanner.close();
}
}
Compile and run the program:
javac LongCompare.java && java LongCompare
You should see output similar to this (your results will depend on the values you enter):
Welcome to the Long Compare Tool!
This program compares two long values that you enter.
----------------------------------------
Enter the first long number: 1500
Enter the second long number: 2000
Result of comparing 1500 and 2000:
1500 is less than 2000
Try running the program again with different inputs:
javac LongCompare.java && java LongCompare
For example, if you enter 5000 and 3000:
Welcome to the Long Compare Tool!
This program compares two long values that you enter.
----------------------------------------
Enter the first long number: 5000
Enter the second long number: 3000
Result of comparing 5000 and 3000:
5000 is greater than 3000
In this example, we're using the Scanner
class to read input from the user. We're also using try-catch blocks to handle potential errors if the user enters invalid input.
The Scanner.nextLong()
method reads a long value from the user, and we then use the Long.compare()
method to compare the two values entered by the user.
This interactive program demonstrates how you can use the Long.compare()
method in a real-world application where user input is involved.