Java Java Basics Perfecting the Prototype Looping until the value passes

M Costa
M Costa
1,433 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,433 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
171,327 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);