Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Basics Perfecting the Prototype Looping until the value passes

Stuck on second part of "Looping until the value passes" test

I watched the video 5 times, still couldn't get it.

instructions - " Prompt the user with the question "Do you understand do while loops?" Store the result in a new String variable named response."

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
console.readLine("Do you understand do while loops?  ");
String response = "No, Yes";
do {
boolean valid = (response = Yes);
 if(valid) {

    console.printf("What did you not understand? Please try again ");
 }

} while(response = "No");

1 Answer

Hey Irakli,

The first part of the challenge instructs you to declare a String variable called response and assign it the value returned from the readLine method.

String response = console.readLine("Do you understand do while loops?");

Next, we need to continually prompt the user with a do while loop. As we set up this do-while structure, we do not need to use a boolean or any conditional statements inside of the loop. The only thing to pay attention to is that we need to move the assignment of the response variable to the inside of the loop (Not the declaration). You should have something similar to this:

String response;
do {
  response = console.readLine("Do you understand do while loops?");
} while (response == "No");

The reason that we need to move the assignment to the inside of the loop is because we want to continuously prompt the user while the response value is equal to "No". So every time through the loop, response is going to be re-assigned a different value of whatever the user enters. It's also important because with a do-while loop, the loop will always run once. This means we will always be able to receive at least one response.

Thanks for clearing that up !! :)