0

Trying to create a simple program that has three options for text input. If the user types one of the three, the program moves on. If the user types something else, the program loops back and asks for the input again until a proper response is given. Using a drop down list or other method will not work, as this is for an assignment.

 System.out.print("Enter one of the following: cheese, water, or burger: ");
        userMedium = user_input.nextLine( );  // store user input as a string
        mediumConvert = userMedium.toLowerCase();

        boolean verifyName;
        if (mediumConvert.equals("cheese") || mediumConvert.equals("water") || mediumConvert.equals("burger")){
            verifyName = false;
        } else {
            verifyName = true;
        }

        while (verifyName = true){
            System.out.println("Please input a valid medium (cheese, water, or burger): ");
            userMedium = user_input.nextLine( );
            mediumConvert = userMedium.toLowerCase();
        }

This is what I have set up so far, but this just keeps repeating the loop OVER AND OVER. After this section I want to execute a switch to work off each of the three correct responses.

I've spent the last hour on google and YouTube, but everything I found is using integers. It seems pretty easy to validate user input when it is just a number and an operand. But how do I use three possible strings?!

3 Answers 3

5
while (verifyName = true)
                  ↑

You're assigning and not comparing. The expression of the assignment returns the assigned value, so your loop is equivalent to:

while (true)

You should change it to:

while (verifyName)

Basically, you should write while (verifyName == true), but it's redundant since it's like asking "Is it true that verifyName has the value true?". Also it prevents potential bugs, like just inserting one = instead of two..

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

2 Comments

Awesome! This worked. I also had to add the boolean change code inside the while statement to get out of the loop. Thanks a bunch!
@JasonGoss Correct, that's another bug. I'm glad it works for you!
2

Noticed two things: 1.) By doing

while(verify = true)

you are actually assigning the value true to verifyName. You need to use

while(verifyName)

2.) Where do you reassign the value of verifyName? You should be validating and reassigning inside the while block.

Also you should consider cleaner alternative solution, but that can wait for another day.

Comments

1

You will never break out of the whileloop because the variable verifyName is never updated inside the loop. This means that you'll either never execute the loop because the user inserted the input you wanted or you'll end up with an infinite loop.

You need to do your input verification inside the loop as well and be careful with the boolean validation as well. Something like:

while (verifyName) {
    System.out.println("Please input a valid medium (air, water, or steel): ");
    userMedium = user_input.nextLine( );
    mediumConvert = userMedium.toLowerCase();
    if (mediumConvert.equals("cheese") || mediumConvert.equals("water") || mediumConvert.equals("burger")){
        verifyName = false;
    } else {
        verifyName = true;
    }
}

Comments

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.