1

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!

user2941526
  • 497
  • 3
  • 6
  • 17

5 Answers5

2

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I've wrapped the code like you said, and I also set each operation to reset the choiceentry back to -1 before the break. However, after some of the operations I get an infinite print repeat of "Enter "1", "2", "3" or "4"", which doesn't allow me enter another option. Surely the next.Int() should shop this happening? – user2941526 Dec 22 '13 at 15:44
1

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();

   ....
}
smk
  • 5,340
  • 5
  • 27
  • 41
0

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.

arcy
  • 12,845
  • 12
  • 58
  • 103
0

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

Muhammad
  • 6,725
  • 5
  • 47
  • 54
0

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;

        }
        }
    }
Anirban Pal
  • 529
  • 4
  • 10
  • Seems like it should work. But as soon as one of the cases finishes, I just get an infinite repeating loop of "Enter "1", "2" or "3" ..Press 0 to quit". – user2941526 Dec 22 '13 at 17:54
  • If user press 4 (some value other than 0,1,2,3 ) then what is expected output(while loop should break or user should get prompt for value input i.e. 0,1,2,3)? – Anirban Pal Dec 22 '13 at 18:09
  • I want to program to force the user to enter 0,1,2 or 3. So it should repeat "Enter "1", "2" or "3" ..Press 0 to quit" and scan the next int entered – user2941526 Dec 22 '13 at 18:18
  • In this code while loop breaks only when user press 0 (quit condition). For all other cases user should get prompt to choose 1,2 or 3. This is the logic you looking for.Am I correct? – Anirban Pal Dec 22 '13 at 18:33
  • Yep, that's right. The code works the first time though. When it comes back to the choice selection it just infinitely repeats "Enter \"1\", \"2\" or \"3\" ..Press 0 to quit" – user2941526 Dec 22 '13 at 18:36
  • It is not infinite repeating. It will repeat if choiceentry is not zero. Output of Above sample code: Enter "1", "2" or "3" ..Press 0 to quit 1 In Case 1 Enter "1", "2" or "3" ..Press 0 to quit 3 In Case 3 Enter "1", "2" or "3" ..Press 0 to quit 4 Enter "1", "2" or "3" ..Press 0 to quit 2 In Case 2 Enter "1", "2" or "3" ..Press 0 to quit 0 Bye Bye – Anirban Pal Dec 22 '13 at 18:43