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 Getting Started with Java Strings, Variables, and Formatting

Call the printf method on the console object and make it write out "<YOUR NAME> can code in Java!"

I just don't fully grasp this task. my code: console.printf("%s can code in Java!");

Name.java
// I have setup a java.io.Console object for you named console
String firstName = "Chris";
console.printf("%s" can code in Java!);

3 Answers

Hi Chris,

You have a couple of points in your code that aren't quite right. First; the output string is terminated too early. By this, I mean that you closed the string, like this "%s". But the rest of the words should be included in it; like this: "%s can code in Java!". Note where the closing inverted commas are placed.

Secondly, the %s placeholder marks a point within that string where the value of a variable will be inserted. In order to identify which variable, the name of the variable is placed within the parentheses of the printf method, separated from the string by a comma.

So, something like this:

String firstName = "Chris";
console.printf("%s can code in Java!", firstName);

This inserts the contents of firstName at the point where the %s is placed inside the output string.

I hope that helps. If not, please ask and I'll do a better job!

Steve.

Micah Hack
Micah Hack
12,902 Points

You're very close! When the code runs Java will insert whatever value has been assigned to firstName in place of the string formatter (%s) You failed to define the (%s) so Java doesnt know what value to use, in this case it should be "firstName" so you can print your message you want in quotes, followed by a comma and then the value. Example: console.printf("%s can code in Java!", firstName)

System.out.prinf method requires a String, and the variable name that will hold the placeholder in the string.

String name = "YOUR_NAME"; System.out.printf("hello %s, name);