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

Got stuck

How can I fixed this? What is an error?

Thank you

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

      if (understand) {
        console.printf("Try again.");
      }
} while (understand);
  String gotIt = console.readLine("Yay!");

1 Answer

Tonnie Fanadez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 Points

Hi Aethaya Meesawat

Thanks for your query.

First of all you should declare the String response and initialize it to input from the Console Object. do-while loop is used to execute a block of statements continuously until the given condition is true. In this case, you should continuously prompt the user if he or she understands the loop until true is returned. The do-while at least runs once and the expression inside the while(expression) must return a boolean value so you don't need to introduce your own boolean value.

Lastly, if the condition tests true you should print out the last statement saying you passed. you can use the %s String Modifier to represent the String.

Please see the code below, hope it helps.

// 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");
  console.printf("Because you said %s, you passed the test!", response);
}
while(response.equals("No"));