user2941526
user2941526

Reputation: 497

JAVA: Menu Infinite-loop

This is a follow up to a question I have asked previously that did get answers that should have fixed my problem, but unfortunately did not. My program reads in a text file and organises data before giving the user a number of options. When the program gets to this point I want to user to be able to select an option, that performs an operations, but then returns the user back to the start point to be able to perform more operations. This is the answer I liked best (thanks to Octopus) and am currently trying to implement.

//set choiceentry to -1, this will make it to enter while loop
 int choiceentry = -1

while(choiceentry < 1 || choiceentry > 3){

        System.out.println("Enter \"1\", \"2\" or \"3\"");
        if(scanchoice.hasNextInt())
        choiceentry = scanchoice.nextInt();

    switch(choiceentry){
        case 1:
           //do logic
           break;
        case 2:
           //do logic
           break;
        case 3:
           //do logic
           break;
    }
}

As I see it, the program should enter the loop initially, allow the user to input a selection, then return back to "enter a value". However, the program does not return, and terminates after one operation. How can I prevent this to continue the program running infinitely?

Thanks in advance!

Upvotes: 1

Views: 1300

Answers (5)

Anirban Pal
Anirban Pal

Reputation: 529

Use break only when user wants to quit(Say when choiceentry=0). You can use "continue" to make loop infinite. Sample code is given for reference

    int choiceentry = 1; // can set any int value except 0 (exit code is 0 for this example)
    Scanner scanchoice = null;

    while (choiceentry != 0) {

        System.out.println("Enter \"1\", \"2\" or \"3\" ..Press 0 to quit");
        scanchoice = new Scanner(System.in);

        if (scanchoice.hasNextInt())
            choiceentry = scanchoice.nextInt();
        // System.out.println("choiceentry=" + choiceentry);

        switch (choiceentry) {

        case 0:

        {
            System.out.println("Bye Bye");
            break;

        }
        case 1:

        {
            System.out.println("In Case 1");
            continue;

        }

        case 2: {
            System.out.println("In Case 2");
            continue;

        }
        case 3: {
            System.out.println("In Case 3");
            continue;

        }
        }
    }

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

The current while loop is there to get valid input -- don't change it.

You need to wrap this code in another while loop that loops til a sentinal value is entered.

while (!isSentinalValue) {
  while (inputNotValid) {
    // get valid input
  }
}

Edit

More specifically in pseudocode:

while (!isSentinalValue) {
  input = invalidValue
  while (inputNotValid) {
     getInput
  }
  use input to do menu things
}

So I would not have the switch block inside of the inner loop, since that loop concerns itself only with making sure that the input entered is valid. Do the switch block outside of the inner loop, and be sure to set the sentintal value that allows the user to escape the outerloop when appropriate.

Upvotes: 2

Muhammad
Muhammad

Reputation: 7324

place the following code after switch

if(choiceentry == 4){
break;
}

Now when you will input 4 then it will be terminated, you can use any value other then 4

Upvotes: 0

smk
smk

Reputation: 5842

Your while(choiceentry < 1 || choiceentry > 3) condition is wrong. If you want it to loop , then you have to make it between 1 and 3 .

So this also means that you will have to change your choiceentry initialization value. This will work.

int choiceentry = 1

while(choiceentry >=1 && choiceentry <= 3){

        System.out.println("Enter \"1\", \"2\" or \"3\"");
        if(scanchoice.hasNextInt())
        choiceentry = scanchoice.nextInt();

   ....
}

Upvotes: 1

arcy
arcy

Reputation: 13123

your loop only runs while choiceentry is less than 1 or greater than 3. As soon as the user enters one of those values, the loop exits.

Learn to use a debugger.

Upvotes: 0

Related Questions