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 Censoring Words - Looping Until the Value Passes

Last Quiz error 2/3

I don't understand what's wrong with this code, I have followed along the videos and look back and forwards on to my code which i did along side the vidoes, but no luck.

help :)

String question; boolean response;

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

String response = question; if(response.equalsIgnoreCase("no."));

{ while(response ); console.printf("try again. \n");

}

}

2 Answers

Vladut Astalos
Vladut Astalos
11,246 Points

You don't need two variables nor an if statement. You only need a variable of type String. Then in your do/while loop you store the answer in that variable of type String and you want to repeat this code as long as the answer is no. Your code should look something like this: String response; do{ response = console.readLine(Do yoiu understand do while loops?); }while(response.equalsIgnoreCase("no"));

Anders Björkland
Anders Björkland
7,481 Points

It's worth pointing out that you also lifted out the String variable response from the do-while loop. Since the condition check on the while statement is outside the scope of the loop it doesn't have access to the variable as long as it is declared within the loop. Now that you declared it before the loop, the condition check has access to it.

Diana Ci
Diana Ci
18,672 Points

'''java String response; do { response = console.readLine("Do you understand do while loops? "); if (response.equalsIgnoreCase("no")) { console.printf("Try again");} } while (response.equalsIgnoreCase("no")); '''