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!

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

   ....
}

2 Comments

Your recommendations are wrong. You've broken his first loop -- which was there to repeat user input if the input was invalid.
So ive edited my answer [which I realized wasnt fully complete] . and this will work.
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.

Comments

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

Comments

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;

        }
        }
    }

6 Comments

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".
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)?
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
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?
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"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.