0

I have a main method as well as a mainMenu method. My goal is to call mainMenu after main is executed. If the user input is not an integer then print that it must be and recall the mainMenu method.

Here are my two methods:

private static Scanner s = new Scanner(System.in);
public static void main(String[] args) {
    System.out.println("Welcome to Sea Bank!\n");
    try {
        TimeUnit.SECONDS.sleep(2);
    } catch(InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    mainMenu();
}

public static void mainMenu() {
    System.out.println("Please select an item below:");
    System.out.println("(0) Login");
    System.out.println("(1) Create Account");
    System.out.print(">> ");
    try {
        int mainMenu = s.nextInt();
        if (mainMenu == 0) {
            login();
        } else if (mainMenu == 1) {
            System.out.println("coo create acc"); //add in account creation
        }
    } catch(InputMismatchException e) {
        System.out.println("\nPlease enter an integer");
        mainMenu();
    }
}

So basically when I call the mainMenu method in main and enter a String instead of an int it returns a StackOverFlowError instead of re-asking for user input for an int.

    ...
    Please enter an integer
    >> 
    Please enter an integer
    >> 
    Please enter an integer
    >> 
    Please enter an integer
    >> 
    Please enter an integer
    >> Exception in thread "main" java.lang.StackOverflowError
        at java.util.regex.Pattern$CharProperty.match(Pattern.java:3776)
        at java.util.regex.Pattern$Branch.match(Pattern.java:4604)
        at java.util.regex.Pattern$GroupHead.match(Pattern.java:4658)
        at java.util.regex.Pattern$Curly.match(Pattern.java:4227)
        at java.util.regex.Pattern$GroupHead.match(Pattern.java:4658)
        at java.util.regex.Pattern$Branch.match(Pattern.java:4604)
        at java.util.regex.Pattern$GroupHead.match(Pattern.java:4658)
        at java.util.regex.Pattern$GroupHead.match(Pattern.java:4658)
        at java.util.regex.Pattern$Ques.match(Pattern.java:4182)
        at java.util.regex.Pattern$GroupHead.match(Pattern.java:4658)
        at java.util.regex.Pattern$GroupHead.match(Pattern.java:4658)
        at java.util.regex.Pattern$Branch.match(Pattern.java:4604)
        at java.util.regex.Matcher.match(Matcher.java:1270)
        at java.util.regex.Matcher.matches(Matcher.java:604)
        at java.util.Scanner.getCompleteTokenInBuffer(Scanner.java:963)
        at java.util.Scanner.next(Scanner.java:1476)
        at java.util.Scanner.nextInt(Scanner.java:2117)
        at java.util.Scanner.nextInt(Scanner.java:2076)
        at main.bank.seabank.Main.mainMenu(Main.java:35)

I'm assuming the user input repeatedly is taking in "Please enter an integer" instead of allowing actual user input. I'm just not quite sure how to fix this.

  • 2
    Possible duplicate of [How to handle infinite loop caused by invalid input (InputMismatchException) using Scanner](https://stackoverflow.com/questions/3572160/how-to-handle-infinite-loop-caused-by-invalid-input-inputmismatchexception-usi) – user7 May 21 '18 at 14:20

2 Answers2

0

To get rid of the user input and avoid the exception, inside your catch block, add:

s.next();
Daniel Almeida
  • 372
  • 1
  • 14
0

I don't think the try-catch is the best way to handle selecion like that. Why don't you just write it like that:

int mainMenu = s.nextInt();
if (mainMenu == 0) {
    login();
} else if (mainMenu == 1) {
    System.out.println("coo create acc"); //add in account creation
} else {
    mainMenu();
}

I'm not 100% sure how it works but I think that the exception is still there unless the application gets to the end of the catch block. If you call mainMenu() inside of the catch block the exception will still be in the memory so as soon as it enters the try-catch it will got straight into catch block again.

Amongalen
  • 2,966
  • 13
  • 20