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

Lots of Errors.

Not really sure where I have gone wrong here, but I am experiencing quite a lot of errors.

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?");
String response;
boolean isInvalidAnswer;
do {
    response = console.readLine("Do you understand do while loops?");
    isInvalidAnswer = (response.equalsIgnoreCase("no"));
    if (isInvalidAnswer)
}
 while(isInvalidAnswer);
                       }

3 Answers

Hi Pierce,

first of all, you don't need to declare your response variable twice. There is no benefit of doing so.

But the true error seems to be when closing your do... while clause. You are trying to open an if statement but it's never opened nor closed correctly. The format of an if-statement is like this:

if (condition) {
  ...
}

Your compiler is probably giving you an error because it cannot find any of the curly braces { } in the line if (isInvalidAnswer). Regardless, in this particular problem you don't need this statement at all -- so just remove it completely.

You close the do... while correctly, however there is an excess closing curly brace } that shouldn't be there. Get rid of that as well. You should also have the while(isInvalidAnswer) on the same row as the other closing curly brace.

Here is the format for the do... while:

do {
  ...
} while (condition);

Here is how your final code should look like:

  // 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");
  } while(isInvalidAnswer);

Hope this helps! :)

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

It looks like you're declaring your variable a few times too many, and there's also an unessecary if condition in there.

Try leaving the variable undeclared outside of the loop, and getting rid of the if statement inside. There's also a closing brace underneath your while loop that needs to be removed.

It should look like

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

Thanks, let me know if this doesn't help.

I've update the code, but I'm still having a minor issue with my boolean statement.