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

alessandro prado
PLUS
alessandro prado
Courses Plus Student 329 Points

Do while loop help

Im not exactly sure if i am doing this activity correctly.

Example.java
// 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

Hey 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'