/* My goal is to use the terminal to receive 2 integers and add them together in a Java program. I need to confirm that both terminal entries are ints. If the are, I should proceed to add the ints together. If not, I should print out "Invalid input entered. Terminating..."
I am trying to use an if statement with hasNextInt(), but my program is only verifying the first Scanner input. How do I confirm both Scanner inputs are ints? Any help would be appreciated.
Here is my Java code: */
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("List of operations: add subtract multiply divide alphabetize");
System.out.println("Enter an operation:");
String operationInput = input.next().toLowerCase();
switch (operationInput){
case "add":// for addition
System.out.print("Enter two integers:");
if (input.hasNextInt() == false) { //confirm the numbers are integers, otherwise terminate
System.out.println("Invalid input entered. Terminating...");
break;
}
else {
int a1 = input.nextInt();
int a2 = input.nextInt();
int at = a1 + a2;
System.out.println("Answer: " + at);
input.close();
break;
}