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

Daniel Avila
PLUS
Daniel Avila
Courses Plus Student 1,474 Points

How do I use console.printf to print out a formatted string that says "Because you said <response>, you passed the test

I'm stuck on the last task and I'm not sure why it is wrong.

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
String response;
boolean loopResponse;
do {
 response=console.readLine("Do you understand do while loops?");
  loopResponse=(response.equalsIgnoreCase("No"));
  if (loopResponse) {
    console.printf("Because you said <response>, you passed the test!\n");
  }
} while(loopResponse);
Stacey Fenton
Stacey Fenton
5,501 Points

My code works - hope it help you!

String response = console.readLine ("Do you understand do while loops?"); boolean repeatResponse;

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

  if (repeatResponse) {
    console.printf ("Try again!");
  }

    }

while (repeatResponse); console.printf ("Good job!");

2 Answers

guessing if you are here you need the last bit

but console.printf(""Because you said " + response + ",you passed the test!");

is not making the tester happy

try instead using %s format string with response as parameter

Seth Kroger
Seth Kroger
56,413 Points

2 things: First, because you are already looping until you get a correct answer, you should print it outside the loop. Second, you need to replace "<response>" with the value of response (printf() has a way to do just that).