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

I am having trouble with Challenge Task 2 of 3 in the last Java Basics challenge.

My code is not working. I don't know what I am doing wrong. Please need some guidance.

// I have initialized a java.io.Console for you. It is in a variable named console.
String response;
do {
  response = console.readLine("Do you understand do while loops?  ");
  if (response.equals("no")) {
} while(response.equals("no"));

2 Answers

Rob Bridges
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,459 Points

Hey there, console is already imported so you don't need to import it, but you should create a variable to hold the answer, and then have it changed throughout the while loop, (which is exactly what you did when you created response)

Next you'll just want to check and make sure that the answer doesn't equal no.

You were super close on this, you just have that if statement in your code that isn't required.


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


You need to create an instance of System.console() like this: Console co = System.console();

the use it like this: response = co.readLine("Do you understand do while loops? ");

your final could shall be:

String response; Console co = System.console(); do { response = co.readLine("Do you understand do while loops? "); if (response.equals("no")) { co.readLine(response); } } while(response.equals("no"));