Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Basics Perfecting the Prototype Looping until the value passes

Samuel Cordova
Samuel Cordova
807 Points

Can't find the correct answer to the last question on java basics.

Can't seem to answer the question that follows "Finally, using console.prinf print out a formatted string that says "Because you said<response>, you passed the test!"

My Code is below:

// I have initialized a java.io.Console for you. It is in a variable named console. String response; boolean isInvalidWord; do { response= console.readLine("Do you understand do while loops?" ); isInvalidWord = (response.equalsIgnoreCase("No")); if (isInvalidWord); { console.printf("Try again. \n\n"); } } while (isInvalidWord); console.printf("Because you said %s, you passed the test!", response); }

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

3 Answers

Steven Parker
Steven Parker
216,810 Points

There is stray extra closing brace on the very last line causing a syntax error. Just remove it and you'll pass.

And while your method works just fine, you don't really need to create an extra variable for testing, you can just include the test expression in the while directly, like this:

do {
    response = console.readLine("Do you understand do while loops?");
} while (response == "No");
Samuel Cordova
Samuel Cordova
807 Points

Thank you! The way you have it is much easier. Do you know why the extra closing brace is not needed? I thought once we open up a code block we had to close it. That's been confusing for me.

Steven Parker
Steven Parker
216,810 Points

You're right, braces must always be used in matching pairs. So here's your code with comments added:

 String response;
 boolean isInvalidWord;
  do {                                       // <-- this brace opens the "do" block
    response= console.readLine("Do you understand do while loops?" );
    isInvalidWord = (response.equalsIgnoreCase("No"));
  if (isInvalidWord); {                      // <-- this brace opens the "if" block
      console.printf("Try again. \n\n"); }   // <-- this brace closes the "if" block
} while (isInvalidWord);                     // <-- this brace closes the "do" block
    console.printf("Because you said %s, you passed the test!", response);
}                                            // <-- this brace has no matching open

You got it! remove the extra closing brace on the last line. Here is another way to solve it!

String response;
boolean isInvalidWord;
do {
  response = console.readLine("Do you understand do while loops?");
  isInvalidWord = (response.equalsIgnoreCase("No"));
} while (isInvalidWord);
console.printf("Because you said %s, you passed the test!", response);
Samuel Cordova
Samuel Cordova
807 Points

Thank you! I appreciate your help. The closing brace concept has been confusing me.

Samuel Cordova
Samuel Cordova
807 Points

Steven Parker, wow! I see it now. Thank you so much for breaking it down like that! You're the best!