0

I'm making a text based adventure game and I needed to make some user input validation.
I have the loops set up for this specific instance but I can't seem to get the loop to break after inputting the correct input.

Code:

    final String KNOCK;
    final String SLAM;
    System.out.println("Do you politely knock on the doors or do you slam them over?");
    System.out.println("Knock or Slam?");
    String decisionOne = userInput.nextLine().toUpperCase();
                
    if (decisionOne.equals("KNOCK"))
        System.out.println("You politely knock on the door. After a minute you decide to push on the handle.  The oak doors swing open, the old rusted iron hinges screaming into the dimly lit hallway in front of you. The Hall is roughly fifty feet long. Three doors on the East wall, and two doors on the west wall. The smell of mold, moisture, and iron hang thick in the air. What used to be a lush red carpet lines the entire length of the hallway up to a narrow set of stairs that reach up to another floor.");
        else if (decisionOne.equals("SLAM"))
            System.out.println("You slam into the heavy oak doors, the heavy iron hinges giving way with a pop. Dust settles around your head as you peer into the first room of The Keep. The Hall is roughly fifty feet long. Three doors on the western wall, and two doors on the eastern wall. The smell of mold, moisture, and iron hang thick in the air. What used to be a lush red carpet lines the entire length of the hallway up to a narrow set of stairs that reach up to another floor.");
        else {
            KNOCK = "KNOCK";
            SLAM = "SLAM";
            do {
                System.out.println("Your options are Knock or Slam. Don't be a rude.");
                decisionOne = userInput.nextLine().toUpperCase();
                if (decisionOne.equals("KNOCK"))
                    System.out.println("You politely knock on the door. After a minute you decide to push on the handle.  The oak doors swing open, the old rusted iron hinges screaming into the dimly lit hallway in front of you. The Hall is roughly fifty feet long. Three doors on the East wall, and two doors on the west wall. The smell of mold, moisture, and iron hang thick in the air. What used to be a lush red carpet lines the entire length of the hallway up to a narrow set of stairs that reach up to another floor.");
                else if (decisionOne.equals("SLAM"))
                    System.out.println("You slam into the heavy oak doors, the heavy iron hinges giving way with a pop. Dust settles around your head as you peer into the first room of The Keep. The Hall is roughly fifty feet long. Three doors on the western wall, and two doors on the eastern wall. The smell of mold, moisture, and iron hang thick in the air. What used to be a lush red carpet lines the entire length of the hallway up to a narrow set of stairs that reach up to another floor.");
            } while ((!decisionOne.equals("KNOCK")) || (!decisionOne.equals("SLAM")));
        }

this is what my output looks like:

Your options are Knock or Slam. Don't be a rude.

slam

You slam into the heavy oak doors, the heavy iron hinges giving way with a pop. Dust settles around your head as you peer into the first room of The Keep. The Hall is roughly fifty feet long. Three doors on the western wall, and two doors on the eastern wall. The smell of mold, moisture, and iron hang thick in the air. What used to be a lush red carpet lines the entire length of the hallway up to a narrow set of stairs that reach up to another floor. Your options are Knock or Slam. Don't be a rude.

the problem is that if you input the correct value the first time then the code proceeds as normal. if you input the incorrect value first then try to input the correct value the loop doesn't break and continues asking for user input without moving on to the next step of the code.

Any help is much appreciated!!

Thank you

Community
  • 1
  • 1

3 Answers3

3

Your do loop runs as long as this condition is true:

while ((!decisionOne.equals("KNOCK")) || (!decisionOne.equals("SLAM")))

Read that to yourself in English... "while decisionOne does not equal KNOCK or decisionOne does not equal SLAM".

Suppose decisionOne is KNOCK. The condition remains true, because decisionOne does not equal SLAM, and the loop continues, forever.

You want an and condition:

while ((!decisionOne.equals("KNOCK")) && (!decisionOne.equals("SLAM")))
Matthew McPeak
  • 17,705
  • 2
  • 27
  • 59
0

The while part of your loop is always going to evaluate to true.

Try:

while(!decisionOne.Equals("KNOCK") && !decisionOne.Equals("SLAM"));
Ellis
  • 568
  • 8
  • 29
0

if you input the incorrect value first then try to input the correct value the loop doesn't break and continues asking for user input without moving on to the next step of the code.

Change:

while ((!decisionOne.equals("KNOCK")) || (!decisionOne.equals("SLAM")));

To:

while ((!decisionOne.equals("KNOCK")) && (!decisionOne.equals("SLAM")));

You only want it to continue looping when the input is not equal to "KNOCK" AND not equal to "SLAM". With || (or) in there, it loops around even when the user enters one of those values...

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40