Skip to content
Advertisement

Verifying multiple tokens as Ints with Java

/* 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;
      }

Advertisement

Answer

You already read the int if it’s available and basically, you’ll just have to repeat this step. You can use an additional flag to indicate whether the program was able to read the two ints successfully:

int a1, a2;
boolean gotTwoInts = false;
if (input.hasNextInt()) {
    a1 = input.nextInt();
}
if (input.hasNextInt()) {
    a2 = input.nextInt();
    gotTwoInts = true;
}

if (!gotTwoInts) {
    System.out.println("Invalid input entered. Terminating... ");
    break;
}

Update

Complete example:

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": { // curly braces because local variables are block scoped.
                System.out.print("Enter two integers:");

                int a1 = -1, a2 = -1; // local variables need to be initialized
                boolean gotTwoInts = false;
                if (input.hasNextInt()) {
                    a1 = input.nextInt();
                }
                if (input.hasNextInt()) {
                    a2 = input.nextInt();
                    gotTwoInts = true;
                }

                if (!gotTwoInts) {
                    System.out.println("Invalid input entered. Terminating... ");
                    break;
                }

                System.out.println("Answer: " + (a1 + a2));
            }
        }
    }
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement