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 trialalessandro prado
Courses Plus Student 329 PointsDo while loop help
Im not exactly sure if i am doing this activity correctly.
// I have initialized a java.io.Console for you. It is in a variable named console.
do
{
String response - console.readLine("Do you understand do while loops?");
if(response.equalsIgnoreCase("No"))
{
String response = console.readLine("What is your question?");
}
while(response.equalsIgnoreCase("Yes"))
{
}
}
1 Answer
jacobproffer
24,604 PointsHey alessandro,
During the first prompt, you of course need to declare a String variable called response and assign it the value returned by the readLine method. Be careful, as you are currently using an incorrect assignment operator. You should use an equal (=) symbol instead of a dash (-).
String response = console.readLine("Do you understand do while loops?");
Prompt two is a little more tricky. You'll want to declare your String variable outside of the loop. But you'll want to assign it a value inside of your do while loop instead of outside. The reason being so that the user will be continuously prompted with the question until the value is equal to 'No'.
An if statement is not needed in our case and neither is a nested while loop. The while goes outside of the loop, near the end. With that said, the end result looks like this:
String response; // Declare your value
do {
response = console.readLine("Do you understand do while loops?"); // Prompt the user
} while ( response == "No" ); // Run the loop while response is equal to 'No'