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 Censoring Words - Looping Until the Value Passes

Klaudia Krol
Klaudia Krol
576 Points

Looping Until the Value Passes - Java

Hi All,

Could you please paste how this exercise should look like ??

I cannot pass this track and dont know where I am wrong .

Thank you in advance , Klaudia

3 Answers

andren
andren
28,558 Points

Sure, I've also added some comments to hopefully make the code a bit clearer for you.

String response = console.readLine("Do you understand do while loops?");
//First prompt the user and store the result to the response String.
do { 
//Start a do loop, do loops run the code in them once,
//then repeats it while the condition it has been given is true.

  response = console.readLine("Do you understand do while loops?"); 
  //Sets the response equal to the prompt again.

} while (response.equals("No")); 
//Here we declare the condition for the do loop to continue.
//We are checking if the response variable is equal to the string "No".

console.printf("Because you said %s, you passed the test!", response);
//Then finally we are printing out a formatted string.
//In a formatted string %s is used as placeholder for string variables.
//The second argument passed to printf is the first variable we want to pass into the placeholder.

Edit: Made some minor changes mainly to clarify some stuff about the do loop.

Paul Yorde
Paul Yorde
10,497 Points
String response = false;
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);

For my example, I asked for the response inside of the loop. The reason for this is in the nature of the do-while loop. Since the do {block} will execute once before it checks the condition of while, we can go ahead and ask for the input after the loop starts—remembering to declare the response variable above the loop;

go ahead and @nickrp @jseifer #bringbackTheShow

Mia Allen
Mia Allen
2,929 Points

try two strings, question and response. it helped me.