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

Treshawn Wilson
Treshawn Wilson
2,307 Points

Can't figure out last question...

its saying my only error is the parenthesis in front of because please help!

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?"); 
} while (response.equals("No"));
    response = console.printf("Because you said <response>, you passed the test!");  

1 Answer

Hey there, Treshawn!

The console.printf() method is used to print out a formatted string. What that means is, you can use a string formatter (%s, %d, %n, etc.) inside of the string and the variable will be printed in its place.

Your code looks great except for that one detail.

The final response after the do-while loop should read:

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

The %s tells the compiler that you plan to pass a string into the printf() method, which will then replace the %s in the sentence "Because you said %s, you passed the test!" After you close the string, you then need to pass additional arguments to the printf() method, equal to the number of formatters you put inside of the string.

If you have

console.printf("%s %s %s %s.");
//This code will not work. There are no arguments yet!

You would need to pass it 4 arguments:

String firstName = "Treshawn";
String lastName = "Wilson";
String verb = "codes";
String adjective = "well";
console.printf("%s %s %s %s.", firstName, lastName, verb, adjective);

This would print "Treshawn Wilson codes well." to the console, because each %s is replaced by the arguments passed in to the printf() method.

More information on the printf() method can be found here: https://docs.oracle.com/javase/7/docs/api/java/io/Console.html#printf(java.lang.String,%20java.lang.Object...)

Hopefully this helps! :-)

Treshawn Wilson
Treshawn Wilson
2,307 Points

Thank you! That actually helped me understand it better.