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

I cannot figure this out

I'm trying to do a do/while loop while continually prompting the user

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
String response = console.readLine("Do you understand do while loops?  ");
boolean No;
do {
  No = console.readLine("response?  ");
  if (No)
        {
                console.printf("I'm sorry.  Please review the content again. \n\n");
          }
} while(No);

1 Answer

Brendon Butler
Brendon Butler
4,254 Points

First off, when creating a variable, it should never start with a capital letter. They should be camelCased. Please take a look at Java Naming Conventions.

The first task asks you to prompt the user whether or not they understand do-while loops. Then, store the response in a variable named "response."

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

The next task asks you to create a do-while loop that will continue to prompt the user

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

The last task asks you to print a formatted string, "Because you said <response>, you passed the test!

console.printf("Because you said %s, you passed the test!", response);

This is what your finished code should look like:

// I have initialized a java.io.Console for you. It is in a variable named console.
String response = console.readLine("Do you understand do while loops? ");

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

console.printf("Because you said %s, you passed the test!", response);