The Scanner class (from java.util) is used to get different types of input
We use System.in to read keyboard input:
Scanner myObj = new Scanner(System.in); // Scanner object
String userInputString = myObj.nextLine(); // method to read user input
int userInputInt = myObj.nextInt(); // another method to read user inputScanner has a few methods to read input besides .nextInt()
Scanner to CalculatorRemember to print out messages to the user.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
double left = scanner.nextDouble();
System.out.print("Enter another number: ");
double right = scanner.nextDouble();
Calculator calc = new Calculator(left, right);
System.out.println(calc);
}What happens if the user does not enter a number?
Write a Java application to check if a year is leap or regular
Leap years are those that are either:
Write a main static method in the same class that asks for user input, and prints out a message saying whether the year entered is leap or not.