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

stenio batista
stenio batista
404 Points

Now continually prompt the user in a do while loop. The loop should continue running as long as the response is No.

BEING 2 DAYS TRYING TO PROMPT THIS BUT NO LUCK!!

/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./

String response;

do {

response = console.readLine("Do you understand do while loops? "); if (response == "no"); console.printf ("try again");

} while (response != "no");

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


/*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.*/

String response;


do {

response = console.readLine("Do you understand do while loops?  ");
  if (response == "no");
  console.printf ("try again"); 

} while (response != "no");

2 Answers

Florian Stegemann
seal-mask
.a{fill-rule:evenodd;}techdegree
Florian Stegemann
Full Stack JavaScript Techdegree Student 22,660 Points

Hi, the loop should run as long as the answer is "No" but your check is currently doing the opposite. Your loop reads like "Do XYZ while answer is not no", which means as long as the user doesn't enter "no", the loop continues running. What you want however is to only keep the loop running if the user actually enters "No", if the user enters anything else (including "Yes") the program should exit the loop. This means you have to check for equality instead of inequality.

Also keep in mind that Java is case sensitive, which means that "no" and "No" are not the same thing.

stenio batista
stenio batista
404 Points

String response;

do {

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

Finnally .. Thnx mate.

String response;

do {

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