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

I am TOTALLY lost! Help!!!!!

I understand how do while loops work but writing out what I understand is becoming a pain. Can some one assist me?! I want to run, or 'do', the question "Do you understand do while loops" 'while' the condition is no. But putting this into works in making me "Hulk-out"! Help please!

// I have initialized a java.io.Console for you. It is in a variable named console.

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

2 Answers

Ryan Ruscett
Ryan Ruscett
23,309 Points

Hey, so a do while as you know works off of a true or false. So whatever the condition in the while portion has to return a false.

So if I do x x == x; This returns true or false. So something like this will work. Now I can't use == to compare an object to text, but I can use .equals. As in my example below.

Basically, I have a response that is a string. I get a value from the console. NOTE it would be a good idea to handle if the answer is upper or lower case, but in this example they are wanting exactly "No" not NO or no.

Then I have a while. The while says response equal to No? it will return yes if true and no if false. So no means it's false and the do portion stops. The only time the loop stops is when I type something other than No. So I put the console.printf after the while loop. I use printf with the %s for string.

Here is the answer

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

} while (response.equals("No"));
console.printf("Because you said %s, you passed the test", response);

Please let me know if this helped you or if you have other questions. Thanks!

Michael Hess
Michael Hess
24,512 Points

Hi SGT Awesome,

The do-while loop is a post-test loop. A post-test loop runs the code inside the loop body at least one time then evaluates the looping condition after the code in the loop body is ran.

//Declare a String variable named response
String response;

// The do while loop keeps asking the same question until "yes" is entered
//So we want to keep repeating when "no" is typed
// Exit the loop when "yes" is entered

//task 2

response = console.readLine("Do you understand do while loops?");  // "yes" or "no" is stored in the response variable 
// when someone types "no" the loop repeats
// when someone types "yes" the loop is exited

If you have any other questions feel free to ask! Happy Coding!