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

M Costa
M Costa
1,621 Points

String print call is failing

Trying to pass in <response> at the end of the loop, but it's printing '<response>' literally, rather than the users response.

I posted something similar a couple of days ago and managed to solve this, but I'm coming across this problem again, not sure if this an issue with scope.

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
boolean failTest;
String response;
        do {
            response = console.readLine("Do you understand do while loops?  ");
           failTest = (response.equalsIgnoreCase("No"));

          if (failTest){
                                  console.printf("Are you sure?, Try again. ");}

        } while (failTest);

console.printf("Because you said <response>, you passed the test!");
M Costa
M Costa
1,621 Points

I've now figured this out using formatted stings, but I was under the impression you could pass console.printf("Because you said <response>) the same way, maybe not

1 Answer

Steven Parker
Steven Parker
229,744 Points

Variable substitution isn't performed automatically when something is enclosed in angle brackets. But you can use a substitution token placed where you want the value shown, and supply the variable as an additional argument:

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