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

Do while loop

The question asks me to prompt the user as long as the answer is no. I have no idea what I'm doing wrong. Please Help.

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


do {
console.printf(response);        
} while (response == "No");

Moderator redacted for explicit answer

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

fodhil zidane Hi there! I'm redacting your comment to remove the explicit answer left. Treehouse discourages the posting of copy/pastable answers with no explanation. Ideally, your answer should be posted in the "Answer" section and contain an explanation with no explicit code or simply some pseudocode.

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! First, the body of the do while is always going to run at least once. My suggestion is to declare the String outside of the do while as you have it, but leave it uninitialized. The problem with your code is that it in the case a user enters "No", it goes into an endless loop. There is nothing in the body of your loop that ever asks for the user to change their answer. The only thing inside the loop is printing out the response which they now have no option to change.

Also note, that while this challenge may pass using the == comparison operator, it is generally better to use the .equals in the case of strings. Comparing strings in Java can have some unintended consequences if using the ==.

Hope this helps! :sparkles:

Thank You!