0

I am writing some Java code where I want to loop back to the earlier method, if an exception is found.

Here is a simplified version of my code:

public class classA
{

   public static void main(String[] args)
   {
      int number1 = askUserForFavoriteSum();
      int number2 = askUserForAnotherNumber();
   }

    public static int askUserForFavoriteSum()
    {
        int firstFavorite;
        int secondFavorite;
        int favoriteSum

        System.out.println("What is your first favorite number?");
        firstFavorite = classB.getIntFromConsole();
        System.out.println("What is your second favorite number?");
        secondFavorite = classB.getIntFromConsole();

        favoriteSum = firstFavorite + secondFavorite;
        return favoriteSum;
    }

    public static int askUserForAnotherNumber()
    {
        int number;
        System.out.println("What is another number?");
        number = classB.getIntFromConsole();
        return number;
    }

}

public class classB
{
    public static int getIntFromConsole()
    {
        Scanner scanner = new Scanner(System.in)
        int value;

        try
        {
            value = Integer.valueOf(scanner.nextLine());
        }
        catch (NumberFormatException e) //Occurs when the input string cannot cleanly be converted to an int.
        {
            System.out.println("ERROR: Invalid input! Please enter a whole number.");
        }
        return value;
    }
}

For example, given the two classes above, I want the following to happen:

  1. askUserForFavorite method is executed.
  2. User enters "Potatoe" when prompted for their first favorite number. As such, a NumberFormatException occurs.
  3. "ERROR: Invalid input! Please enter a whole number." is printed to the console.
  4. askUserForFavorite method is executed (again). This is what I am struggling with.
  5. User enters "2" when prompted for favorite number. As such, no exception occurs.

After doing some research, I see that many people go with the Strategy Design Pattern, however, that doesn't suit me well because I have dynamic variables in my classA methods and interfaces don't play well with dynamic variables.

In other cases, some developers pass a "Callable < MethodReturnType >" parameter into their equivalent of my "getIntFromConsole" method and call a future method that mirrors the passed-in method. This approach seems like it would work, but it requires me to add a new layer of abstraction.

Is there a way in which I can simply call the previous method from classA that called my getIntFromConsole method, without adding a layer of abstraction or an interface?

Community
  • 1
  • 1
  • use a while loop (i.e. `while (invalid) { askForIt }`) –  Feb 16 '17 at 06:11
  • You can make `getIntFromConsole` throw `NumberFormatException` further (possibly wrapping it into a better-suited `Exception`), catch it in `main` and then retry (e.g. using a loop). – yeputons Feb 16 '17 at 06:11

1 Answers1

2

Just change it to loop

public static int getIntFromConsole()
{
    Scanner scanner = new Scanner(System.in)
    int value;

    while (true) {
      try
      {
          value = Integer.valueOf(scanner.nextLine());
          break;
      }
      catch (NumberFormatException e) 
      {
         System.out.println("ERROR: Invalid input! Please enter a whole number.");
      }
    }
    return value;
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64