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

string Formatter

Hi. I am having a problrm with: Now replace <YOUR NAME> in the console.printf expression with the firstName variable using the string formatter.

Name.java
// I have setup a java.io.Console object for you named console
String firstName="ken";
console.printf("ken can code in java!");
console.prinf("my name is %s\n");

2 Answers

Allan Clark
Allan Clark
10,810 Points

You told the formatter where the string variable will go but not what variable to put there. Add the firstName variable to your arguments for printf(). Don't forget the comma! :D

Hi Kenneth,

It looks like your code was good for the first two tasks. Let's take another look at task 3.

Now replace <YOUR NAME> in the console.printf expression with the firstName variable using the string formatter.

We just need one printf statement for this.

String firstName = "ken";
console.printf("%s can code in Java!", /* what goes here? */);

When passing in a formatted string, printf takes comma separated arguments. The first argument is the string with placeholders, like %s. Then, each additional argument represents the values to insert into the string at those placeholders.

String food = "pizza";
console.printf("I love %s.", food);

// output: I love pizza.

Hope this helps,

Cheers