0

so I'm kind of new to catching errors and such. Anyways, the program is supposed to ask the user for 2 integers and then add them together. Simple, but if either of the numbers are not integers, than an error is thrown. Currently, if I enter 2 integers, instead of adding them, it just restarts the getAnswer() method and outputs them again. Also, if you enter more than one error, it will simply exit.

package javaapplication1;

import java.util.InputMismatchException;
import java.util.Scanner;


public class JavaApplication1 {

  public static void main(String[] args) {
    Intro();
    System.out.println("Your answer: "+getAnswer());
  }
  private static void Intro() {
    System.out.println("Hello this program adds 2 integers together and catches errors.");
    getAnswer();
  }
  private static int getAnswer() throws InputMismatchException {
    Scanner scanner = new Scanner(System.in);
    try {
      System.out.println("Please input a number");
      int num1 = scanner.nextInt();
      System.out.println("Please input a second number");
      int num2 = scanner.nextInt();
      return num1+ num2;
    } catch (InputMismatchException exp) {
      System.out.println("Exception thrown");
      return 0;
    }
  }
}
5
  • Have you tried putting a debug point in the getAnswer() method and stepping through it? Also, if you catch a particular kind of exception, the method signature no longer needs to say throws <whatever>Exception. Commented Apr 13, 2017 at 23:03
  • 2
    "Currently, if I enter 2 integers, instead of adding them, it just restarts the getAnswer() method and outputs them again" - yeah, because your program contains two calls to getAnswer, and you throw the first result away. Commented Apr 13, 2017 at 23:04
  • Do you not want it to exit on error? Commented Apr 13, 2017 at 23:05
  • There is no question in this question... Commented Apr 13, 2017 at 23:05
  • The name Intro violates the Java naming conventions. Commented Apr 13, 2017 at 23:42

1 Answer 1

3

You are calling getAnswer(); totally two times, so you just remove the call from Intro() method which will solve the problem.

private static void Intro() {
        System.out.println("Hello this program adds 2 
             integers together and catches errors.");
}

If you want to prompt the user to reenter the input again, you can call the getAnswer() in the catch block as shown below:

private static int getAnswer() {
        Scanner scanner = new Scanner(System.in);
        try {
        System.out.println("Please input a number");
        int num1 = scanner.nextInt();
        System.out.println("Please input a second number");
        int num2 = scanner.nextInt();
       return num1+ num2;
         } catch (InputMismatchException exp) {
           System.out.println("Exception thrown, please reenter values:");
           getAnswer();
        }
       return 0;
 }

One more point is that rather than catching the InputMismatchException, the other better way is read the inputs as strings and validate that they contain only numeric values like below:

private static int getAnswer() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please input a number");
        String num1 = scanner.nextLine();
        System.out.println("Please input a second number");
        String num2 = scanner.nextLine();
        if(num1.matches("[0-9]+") && num2.matches("[0-9]+")) {
            return Integer.parseInt(num1)+ Integer.parseInt(num2);
        } else {
            System.out.println(" Your inputs contain Invalid characters");
            getAnswer();
        }
       return 0;
    }
Sign up to request clarification or add additional context in comments.

8 Comments

And under the catch block he should change the return to return getAnswer(); if he wants it to keep running and ask again.
Ah, fixed that issue, but now it exits when the program catches the exception.
really shouldn't use catching the exception as the control behavior for asking for correct input. Should read in both values and verify they are integers, then continue else output incorrect input and retry.
I appreciate it, it's been a long day, I thought exceptions were basically supposed to restart the method they were thrown from?
@RAZ_Muh_Taz I agree with you
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.