0

But if I put it to "valid = false;" it does not work in debug or running.

In fact even running the code, I can't type anything after the "Do you want to order anything else?", no matter if it's in debug or running mode.

Am I missing something? After asking "how many you want to order" and you put in a number after it should ask "do you want to order anything else" which is does but then I can't type and break out of the do while loop. Everything else is working up to that point.

do {


boolean itemValid = true;
    while (itemValid) {

    System.out.println("Please enter an item name: ");

    String enterItem = scnr.nextLine();


        if (keepTrack.containsKey(enterItem)) {

             System.out.println(keepTrack.get(enterItem));
             itemValid = false;

        } else {
             System.out.println("Sorry we don't exist.");
             continue;

        }


           System.out.println("How many do you want to order?");
           int enterQuan = scnr.nextInt();
           yourOrder = enterQuan;
           valid = false;

            }

           System.out.println("Do you want to order anything else?");
           String yesNo = scnr.nextLine();

             if (yesNo.equalsIgnoreCase("n")) {
                 valid = false;
           } else
                break;

} while (valid);

1 Answers1

0

Two problems with your code. First, probably unnoticed yet:

 do ...

 if (keepTrack.containsKey(enterItem)) {
         System.out.println(keepTrack.get(enterItem));
         itemValid = false;
    } else {
         System.out.println("Sorry we don't exist.");
         continue;
    }

When your input is "invalid", you turn into the else branch. The else branch continues the loop. The loop depends on value. Thus: as soon as you start with value=true, and then have an invalid input, you end up with a never-ending loop. Because nothing between the loop start and the continue statement will ever change the conditions that would end the loop.

Your actual question: when you call int enterQuan = scnr.nextInt() that does not consume the "ENTER" that you typed on the console. See here for details.

And there is another problem:

if (yesNo.equalsIgnoreCase("n")) { valid = false; } else break; }

When the user enters n or N, you go valid=false which ends the outer do-while loop. Thus: when the user enters anything else, the elsepath is taken. What is to be found in the else path? A break. Which also ends the do-while loop.

In other words: your code does exactly what you told it to do: to end the do-while loop, one way or the other.

The real answer is: you need to be much more careful what you put in your code. Each and any character matters. And when you put something into your code for an experiment: remember that it is there, and has effects.

GhostCat
  • 127,190
  • 21
  • 146
  • 218
  • However now it always exits out even if I type yes. I've tried to else continue; in the yesNo if branch at the bottom. – JackieTowns Jul 04 '19 at 19:21
  • @JackieTowns I updated the answer accordingly. I hope that all your problems are solved now. So please consider upvoting/accepting at some point. – GhostCat Jul 05 '19 at 05:55