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

Andrew Wilkerson
Andrew Wilkerson
1,504 Points

Nothing outputs to output.html

I can get nothing to output on the preview section. I have tried just adding "console.printf("hello");" even before the loop starts but nothing comes out.

pet
pet
10,908 Points

Could you post your code please Andrew Wilkerson :).

Andrew Wilkerson
Andrew Wilkerson
1,504 Points

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

String response;
boolean answer;
do {
        response = console.readLine("Do you understand do while loops?  ");
    answer = response.equalsIgnoreCase("no");
} while (answer);

console.printf("Becuase you said " + response + ", you passed the test!");

After putting this into an IDE, it worked. So I am lost.

1 Answer

Hello!, how your doing?

Lets walk through this code challange with 3 steps :} :

1. first we are asked to declare a variable called response and prompt the user:

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

2. then we need to use the variable in a do while loop and that's where your problem is, do while loop let u run a piece of code at least once even if the result is false, you use a condition that u want to keep running as long as the condition in the while block is false, in this example as long as the response from the user is "No" the user will be prompt again and again and again..... now we understand what a do while loop is, the answer variable is not neccessary in this example u just need to compare response to "No".

this is the complete code:

String response;
do
{
    response =  console.readLine("Do you understand do while loops?");
}while(response == "No");
console.printf("Because you said %s, you passed the test!", response);

i hope this clear things for u.

Andrew Wilkerson
Andrew Wilkerson
1,504 Points

that does make sense, i guess I just don't rememeber the part of the lesson that covers (response == "No"); in the videos it was about using response.equals();

Thanks for the help!