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!

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

Kenneth Kimbrell
Kenneth Kimbrell
Courses Plus Student 6,816 Points

Having problem with this workshop on Java Basics

I am getting a syntax error and I am unsure where I am going wrong. Can you please help

// 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 wrongAgain;
do {
  response = console.readLine("Do you understand do while loops? ");
  wrongAgain =(response.equalsIgnoreCase("No");
               if (wrongAgain) {
               console.printf("No is not an accepted answer");
response = console.readLine("Do you understand do while loops? ");

1 Answer

Ryan Ruscett
Ryan Ruscett
23,308 Points

Woah, way to much code.

So a do while as you know works off of a true or false. So whatever the condition in the while portion has to return a false. A boolean is obvious but not the only thing that you can use in this instance.

If I do x x == x; This returns true or false. So something like this will work. Now I can't use == to compare an object to text, but I can use .equals. As in my example below.

Basically, I have a response that is a string. I get a value from the console. NOTE it would be a good idea to handle if the answer is upper or lower case, but in this example they are wanting exactly "No" not NO or no.

Then I have a while. The while says response equal to No? it will return yes if true and no if false. So no means it's false and the do portion stops. The only time the loop stops is when I type something other than No. So I put the console.printf after the while loop. I use printf with the %s for string.

Here is the answer

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

} while (response.equals("No"));
console.printf("Because you said %s, you passed the test", response);