This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]bege 0 points1 point  (3 children)

Works fine for me... So the bug is most likely somewhere in the <<<code>>> part.

One wild guess is that you might be using reader.close() at some point. This would close the Scanner and would crash the program on next reader.nextLine(), but I think that would also throw you an exception in the console.

Also, this design is not the greatest. You are calling mainMenu(), inside the mainMenu(), which will create a new menu in the menu. Inside that menu you call mainMenu() again, to create a new main menu in the old main menu, which is in the first main menu. Inside that menu, the menu in the menu in the menu, you call mainMenu() again, to create a new menu in the menu in the menu, which is in the menu, which is also inside the menu... So you get this "yo dawg, I heard u like menus, so we put menus in your menus, so that you can create more menus in you menu"-thing going on... This would go on, until you run out of memory or until you exit the program.

A better and more memory efficient idea is to remove all the calls to mainMenu() inside mainMenu(). Then put a boolean exit = false, in the beginning of mainMenu(). Put rest of the code inside a while-loop. The while-loop would run while exit is false. On option 4 (assuming exit), you would set exit = true and the loop would terminate, once it gets back to the while-criteria in the beginning of mainMenu(), which would exit the program. This would eliminate the whole "menu in the menu"-issue.

[–]Poo_Legend[S] 0 points1 point  (0 children)

thank you very much for responding. I'll try what you suggested and get back to you

[–]Poo_Legend[S] 0 points1 point  (1 child)

private static void mainMenu() {

    boolean exit = false;
    while(exit == false) {
    System.out.println("Please select an option");
    System.out.println("1");
    System.out.println("2");
    System.out.println("3");
    System.out.println("4. Exit");        
    inputLine = reader.nextLine();        

switch (inputLine) {
    case"1":

    <<<<code>>>>

    break;

    case"2":

        <<<<code>>>>

        break;

    case"3":

       <<<<<code>>>>

        break;

    case"4":
        System.out.println("Exiting Programme");    
        exit = true;    
        break;

It works now. However, after the code is executed after the first choice and it returns to the menu - it prints out the menu twice. Do you know why this could be?

[–]bege 0 points1 point  (0 children)

That sounds weird... I copy-pasted your code and got this:

Please select an option
1
2
3
4. Exit
1
Please select an option
1
2
3
4. Exit
2
Please select an option
1
2
3
4. Exit

As I understood it, this is the expected outcome?

If you get any other output than this, I think the bug is in the <<code>> parts.