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

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!