Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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