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

Captain Moomie
PLUS
Captain Moomie
Courses Plus Student 589 Points

I am completely stuck on this loop

It says "Now continually prompt the user in a do while loop. The loop should continue running as long as the response is No. Don't forget to declare response outside of the do while loop." I'm just completely lost and I have no idea where to go from here.

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

2 Answers

rydavim
rydavim
18,814 Points

The next step is asking you to prompt the user to see if they understand do-while loops, and to keep asking until they don't answer No. A do-while loop in Java looks like this:

do {
     statement(s)
} while (expression);

In this case, you'll want to prompt the user for their answer inside the loop, and then check to see if their answer was No. If so, you'll want to continue prompting them. Try using this pseudo-code to get started.

do {
     // Prompt the user whether they understand do-while loops.
    // You've already done something very similar in the first step.
} while (expression); // This expression should check whether their response is "No".
// If their response was "No", continue the loop. 

If you're still stuck, let me know and we can walk through a solution. Java isn't my strongest language, but I'll do my best to explain. Good luck, and happy coding!

Captain Moomie
Captain Moomie
Courses Plus Student 589 Points

I kind of understand that part, but I'm mainly confused about the statement(s).

rydavim
rydavim
18,814 Points

Basically, they want you to put the statement you wrote in step one inside the loop so you can run it multiple times.

// You're gonna' run the prompt inside the loop, but you still need the variable out here.
String response;

do {
    // You wrote this correctly already. Nice job!
    response = console.readLine("Do you understand do while loops?");
} while (response == "No");

Statement(s) really just refers to the stuff you want to do inside the loop. In this case, prompt the user to see if they understand loops.

Ale Glass
Ale Glass
419 Points

rydavim I have a question. According to your answer the last line says:

while (response == "No"); but according to video the format would be: while (response.equals("No")); Both are correct answers when you compile it, is just another way to express it? what's the difference?

rydavim
rydavim
18,814 Points

That's a fantastic question, that I'm afraid I don't have a great answer for. In doing a little bit of reading, I would go with the format given in the video. The short answer is that == checks to see if both variables refer to the same object (address comparison), and .equals() evaluates to the comparison of values in the objects (content comparison). Additionally, the first is an operator and the second is a method.

Unfortunately, Java is not my strongest language so I'm not confident in my ability to clarify further. If you're interested in doing some more reading, this GeeksforGeeks page and this StackOverflow thread seemed the most helpful to me.