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

Arnav Singh
Arnav Singh
636 Points

Java basics 'do while'condition codeChallenge problem

PLS help me correct this code,have been stuck on this for a while. response=console.readLine("Do you understand do while loops? "); String response; boolean isWrong; do{ response=console.readLine("Do you understand do while loops? "); isWrong=(response.equalsIgnoreCase("No")); if (isWrong){ console.printf("Why"); } while (response); String response=console.readLine("Do you understand do while loops? ");

Yixi Guan
Yixi Guan
7,927 Points

It's a little difficult to read your code. You can read this post and wrap your code. So it will appear in a black box.

https://teamtreehouse.com/community/posting-code-to-the-forum

1 Answer

Rebecca Rich
PLUS
Rebecca Rich
Courses Plus Student 8,592 Points

Hi Arnav,

So the challenge is asking you to keep checking for a console response that isn't "No" using a do while loop, and then eventually when a response is entered to print out the formatted String to the console.

If we just want to prompt a user for their response from the console, we can use something like:

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

This will store their response in the response String. The challenge goes a step further and asks you to keep asking that question until the user responds with something other than "No" ...

The key to a do-while loop is that the do block (the stuff in between the {} after "do") is always completed once before the while loop condition is even checked. If the while loop condition passes it will re-run the code in the do block. This is great for this problem because we will never have an answer that isn't "No" if we don't first prompt the user for their response.

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

So now we first ask the user "Do you understand do while loops?" If they respond with no (i.e. response.equals("No") returns true), we repeat and ask them again. If the response isn't No, we exit the do-while loop and move along in the code. Notice I have moved the response definition outside of the do-while loop. This is so that we can access the response variable later in the code, outside of the do-while loop. This concept has to do with variable scope. Also note that there is a semicolon at the end of the while line.

Now for the last part... using console.printf print out a formatted string that says "Because you said <response>, you passed the test!"

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);

We can add the console print statement after the do-while loop. By the time we get to that place in the code we know we have gotten a response other that "No" and have exited the do-while loop. We can use the %s string "placeholder" to put the response in the string, as it was formatted in the challenge prompt.

Hope this helps! Let us know if you have any additional questions!

Arnav Singh
Arnav Singh
636 Points

thanx, finally done!!!