0

I have a question, How can i do so that all input that is not a number int is ignored.

for example:

option 1(add) "Enter two integers: " input: asdf (<--asking for this to be ignored) input: 1 input: 2 output: 3

I believe that this validation would be done inside the case switch, and ive done some googling about parse and about exceptions but they dont quite cut what im looking for.

suggestions, tips, and critique are welcome.

thanks in advance!

 public class MathTeacher {


  public static int addNumbers(int n1, int n2){
  int add = n1+n2;
  return add;
  }

  public static int subtractNumbers(int n1, int n2){
  int subs = n1-n2;
return subs;
  }

  public static int multiplyNumbers(int n1, int n2){
  int mulp = n1*n2;
return mulp;
  }

  public static int divideNumbers(int n1, int n2){
  int div = n1/n2;
  return div;
  }
  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    while(true) {
      int choices;
      System.out.println(
          "Welcome to *Mental Math Practice* where you can test your addition, subtraction, multiplication, and division.");
      System.out.println("Enter 1 to add the two numbers.");
      System.out.println("Enter 2 to subtract the second number from the first number.");
      System.out.println("Enter 3 to multiply the two numbers.");
      System.out.println("Enter 4 to divide the first number by the second number.");

      choices = scanner.nextInt();

      switch(choices){
        case 1: {
          System.out.println("Enter two integers: ");
          int n1 = scanner.nextInt();
          int n2 = scanner.nextInt();
          int add = addNumbers(n1,n2);
          System.out.println(add);
          continue;
        }
        case 2: {
          System.out.println("Enter two integers: ");
          int n1 = scanner.nextInt();
          int n2 = scanner.nextInt();
          int sub = subtractNumbers(n1,n2);
          System.out.println(sub);
          continue;
        }
        case 3: {
          System.out.println("Enter two integers: ");
          int n1 = scanner.nextInt();
          int n2 = scanner.nextInt();
          int mulp = multiplyNumbers(n1,n2);
          System.out.println(mulp);
          continue;
        }
        case 4: {
          System.out.println("Enter two integers: ");
          int n1 = scanner.nextInt();
          int n2 = scanner.nextInt();
          int div = divideNumbers(n1,n2);
          System.out.println(div);
          continue;
        }
      }
      System.out.println("Enter 'Quit' to end the program.");
    }

  }

}
  • You can simply call `scanner.nextLine()` and ignore it, skipping that line permanently. (you don't have to use it you know) – FailingCoder Oct 12 '19 at 21:01
  • Possible duplicate of [How to check type of variable in Java?](https://stackoverflow.com/questions/3993982/how-to-check-type-of-variable-in-java) –  Oct 12 '19 at 21:03

1 Answers1

0

If I'm understanding the question correctly, you'd like to repeatedly ask the user for input until they provide an integer.

The method below will ask the user for input until they enter a string that can be parsed into an int without throwing an exception. Once a valid int is provided, the loop will break.

private static int getIntFromUser(Scanner scan) {
    int x = 0;
    while (true) {
        try {
            System.out.println("Enter an integer: ");
            x = scan.nextInt();
            break;
        } catch (InputMismatchException e) {
            scan.next();
        }
    }
    return x;
}

Now, retrieving input for two numbers would look something like this:

int n1 = getIntFromUser(scan);
int n2 = getIntFromUser(scan);
Anil
  • 655
  • 1
  • 11
  • 25