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

Wei Guan
Wei Guan
403 Points

Hi, how do I solve this challenge?

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 response; do { response = console.readLine("Do you understand do while loop?"); if(response.equalsIgnoreCase("No")); } } while (response.equalsIgnoreCase("No"));

Keep getting compiler error. Please help, really got stocked here...

1 Answer

Ok so first off please when you past code use the markdown so its not horrible to read :P

Here is what you said

String response; //1
do { //2
response = console.readLine("Do you understand do while loop?"); //3
if(response.equalsIgnoreCase("No")); } } //4
while (response.equalsIgnoreCase("No"));//5

So here is what I think you were trying to write

String response;
do{
response = console.readLine("Do you understand do while loop?");
System.out.println(response);
}while(!response.equalsIgnoreCase("No"));

So let's break down where you went wrong here.

at line 4 you have an unnecessary if statement you dont need to check if its equal to no that is what the do while is supposed to do.

Also on line 4 the scope of that if statement is mistyped it should be {} not } } generally you also want something to execute inside of your scope.

On line 5 you want to continue to do this while it IS NOT equal to NO so make sure you have an ! to indicate that you want it when it is NOT, not when it IS.

Wei Guan
Wei Guan
403 Points

Thank you for your answer!!! Your answer is so detailed. It’s my first time asking question here, I can already feel the quality of this community. Jordan your the best .