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

Luuk de Reus
Luuk de Reus
1,936 Points

What did i do wrong? (JAVA LOOP)

Pls help

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

1 Answer

Jack Hinchliffe-wood
Jack Hinchliffe-wood
4,958 Points

You are almost correct with your answer, you just appear to have missed a few details from the question.

The question asks you to return the response that breaks the do while loop in a formatted string.

In this case that response is anything other than "No", as that is the response that the while loop is checking for, so there is no need to create a separate if statement like you have in your example.

// 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 while loops?");
} while (response.equals("No"));
console.printf("Because you said <%s>, you passed the test!", response);

So here we have a console.printf, which takes the response variable that has been set through the console.readLine method on the previous line and then using the <%s> String formatter we can include that response to allow the console to return to the user the response they sent.

Hope this helps.

Jack