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

Stevano Jones
Stevano Jones
507 Points

I'm so close but can't find the missing do while loop answer

when I switched my No answer restriction to yes it suddenly said my previous task was incorrect when it wasn't so I need help!

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
String response;
boolean isInvalidAnswer;
  do  {
    response = console.readLine("Do you understand do while loops?  ");
    isInvalidAnswer = (response.equalsIgnoreCase("No"));
  if (isInvalidAnswer)  {
      response.equalsIgnoreCase("Yes");
      response.equalsIgnoreCase("No"); 
    console.printf("Because you said Yes you passed the test!");
    console.printf("Please try again.");
  }
 }  while (response.equalsIgnoreCase("Yes"));

2 Answers

Rafael Almeida
Rafael Almeida
1,365 Points

Here is what i think:

String response;

boolean isInvalidAnswer;

do {

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


isInvalidAnswer = (response.equalsIgnoreCase("No"));  // if response = No; then isInvalidAnswer will be = true

if (isInvalidAnswer) { // isInvalidAnswer is True and will do the following till it is true

  response.equalsIgnoreCase("Yes");            // take this line out of the code
  response.equalsIgnoreCase("No");             // take this line oout of the code

console.printf("Because you said Yes you passed the test!");    //take this line out of the code and create an else (in case the if is not true)
console.printf("Please try again.");                                              // it's OK this one

}

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

} while (response.equalsIgnoreCase("Yes")); //it must be No for the loop work

Stevano Jones
Stevano Jones
507 Points

I used the your idea is still says that task 2 is no longer correct so I need to make the answer connect to my 2nd task answer somehow.

Rafael Almeida
Rafael Almeida
1,365 Points

I've past the challenge myself with this code, i will write it the way It must be after the alterations:

String response;

boolean isInvalidAnswer;

do {

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

isInvalidAnswer = (response.equalsIgnoreCase("No")); // if response = No; then isInvalidAnswer will be = true if (isInvalidAnswer) { // isInvalidAnswer is True and will do the following till it is true console.printf("Please try again."); // it's OK this one }

    else {                                                                                   // in case boolean is False
   console.printf("Because you said %s you passed the test!", response);                // boolean false = do this
                }

} while (response.equalsIgnoreCase("No")); //it must be No for the loop work