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

Been stuck on this question for hours... can't figure out what my errors are. Please help?

QUESTION: Now continually prompt the user in a do while loop. The loop should continue running as long as the response is No. Don't forget to declare response outside of the do while loop.

MY ANSWER: String questionAsString = console.readLine("Do you understand do while loops?"); String response = ("Enter your response: "); boolean isInvalidWord; response = ("Enter your response: "); do { isInvalidWord = (response.equalsIgnoreCase("no")); if (isInvalidWord); console.printf("That answer is incorrect. Try Again. \n\n"); }


ERRORS:

JavaTester.java:127: error: while expected } ^ JavaTester.java:131: error: illegal start of expression taskNumber = 1; ^ JavaTester.java:131: error: ')' expected taskNumber = 1; ^ 3 errors

There are definitely some errors in your code. Try breaking down the error messages you get to see what the compiler is trying to tell you. In seems in this instance you might be missing some closing braces and closing parentheses, maybe at line 127 and line 131. I'm not sure what taskNumber = 1 is referring to since you didn't mention it anywhere in the code you shared. Other mistakes in your syntax and logic include forgetting code body for your if statement and forgetting while() at the end of your do body.

My suggestion would be to try something like the following:

String response;

do { response = console.readline("Do you understand do while loops?"); } while (!response.equalsIgnoreCase("no"));

I think that's what you're trying to accomplish, anyway. (*edited to make loop continue to run while response is not equal to "no")

I still can't figure it out, I've ran through the video so many times and still can't figure out what I'm doing wrong...

1 Answer

Did you try what I wrote down? I don't have a Java compiler on my computer to test it out, but I imagine it should do what you need it to do.

Try this and see if it works:

String response;

do { 
    response = console.readline("Do you understand do while loops?"); 
} while ( !response.equalsIgnoreCase("no") );

added: "readLine" and not "readline".

Also removed the "!" after the while (don't think "!" is necessary")

Then it worked for me! :)

String response;

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