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 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.

Example.java
// 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")) {
    console.readLine(response);
  }
} while(response.equals("no"));

2 Answers

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 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.

Try

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

Thanks.

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"));