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

Don't understand why I can't get this do while loop to work I did the same thing as the guy in the video and no luck.

i'm trying to make this work but i'm hsving no luck at all I don't understand what i'm doing wrong I need to make it keep running if someone says no.

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 isAnswerValid = (response.equalsIgnoreCase("yes"));
  if (isAnswerValid) {
    console.printf("you do understand do while loops");
    } while(isAnswerValid);
}
Gábor Molnár
Gábor Molnár
9,928 Points

The problem is that you need to add while to the do statement and not to the if . So a do-while loop looks like this correctly:

do {
  // some kind of code here
} while ( /* here some condition */ ) 

Here is the solution for your problem:

  // Initialize response as string
  String response;
do {
  // Wait for a console input from the user
  response = console.readLine("Do you understand do while loops");
 // If the answer is yes (in any kind of case), isAnswerValid will be true, otherwise it will be false
  boolean isAnswerValid = (response.equalsIgnoreCase("yes"));
  if (isAnswerValid) { 
    console.printf("you do understand do while loops"); // this will be printed if the isAnswerValid is true
  }
} while(isAnswerValid) // if this is true, then do block will run again 

This loop firstly runs and asks for a question and wait for an input. Then it will be running until the condition in the while won't be false.