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

<SOLVED> Please help been stuck for 3 days

I cant seem to find how to do this , could someone please help ?? Awnsers would be awsome and help heaps so i can see where i went wrong thank you !

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
String response;
do{
response = console.readLine("Do you understand do while loops?");
}
if(response){
  console.print("Good job bud"); 
} 
while (response.equalsIgnoreCase("No"));

3 Answers

Your while-condition looks good!

Here a few points:

  • use indentation everywhere, it helps you identify problems with your blocks/loops/if-else (for example the first line after your do statement has not identation)

  • you are putting the "Good job bud" INSIDE your do-while loop! That is wrong, and you would get that sentence even if "No" was entered (and not if something else was entered) => so you need to put it out of the loop (underneath the while... because the loop ends with the while

  • for better clarity, close the bracket on the same line as the while-statement (as the first character like in my code)

  • your if statement is wrong. And in fact you do not need an if-statement at all. You can just put the print-statement AFTER the do-while loop, because you know that if code is executed after the do-while loop it means that it passed the response was not "No"

// 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?");

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

console.printf("Because you said %s, you passed the test!", response); 

Oh sorry... I was a bit late...

No no its fine , thank you for going out of your way to give an awsome in depth and detailed note of my work, it helps alot and is awsome for learning from , thank you

No no its fine , thank you for going out of your way to give an awsome in depth and detailed note of my work, it helps alot and is awsome for learning from , thank you

Thanks for being thankful, this is always nice to see and encouraging to continue :)

Paul Penketh
PLUS
Paul Penketh
Courses Plus Student 10,165 Points

Hi, you should move the console.printf outside of the do while loop - you know they have entered something other than "No" if they get to this point otherwise they will still be in the loop.

// I have initialized a java.io.Console for you. It is in a variable named console.
String response;
do{
  response = console.readLine("Do you understand do while loops?");
}
while (response.equalsIgnoreCase("No"));
console.printf("Because you said %s, you passed the test!", response); 

Hope that helps!

Thanks paul that is alot of help , now i can finnally move on haha , im glad i understand it better and can see where i was going wrong thanks heaps !!

anil rahman
anil rahman
7,786 Points

So first make the string and save the response of the prompt

String response;

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

Then add a do while loop and keep prompting while response is no

String response; 
//declare outside the loop so can use anywhere and not being declared and set inside the loop.

do
{
   response = console.readLine("Do you understand do while loops?"); 
   //keep looping and prompting and then save the response when its not no
}

while (response.equalsIgnoreCase("No")); //while response no

Then add a console printf saying some stuff that was written in the question.

String response; 
//declare outside the loop so can use anywhere and not being declared and set inside the loop.

do
{
   response = console.readLine("Do you understand do while loops?"); 
   //keep looping and prompting and then save the response when its not no
}

while (response.equalsIgnoreCase("No")); //while response no

console.printf("Because you said %s, you passed the test",response);
//Here we are just printing and replacing the placeholder %s with the variable value response