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

What does it mean by replace your name with the firstName variable using the string formatter?

I have this

    String firstName = "Melissa";
    console.printf("%s + can code in Java");

from the previous challenge tasks, but I don't know what the third challenge task means. I tried doing this

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

but it wasn't correct.

3 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Melissa;

You need to tell the string formatter what to use in it's place. In looking at the example above, after the "The %s ate my homework" one would put , animal before the );. This tells Java that when it is processing the printf function to replace the %s with the variable animal, in this case.

So, for the challenge you are very close, you just need to add a comma and the firstName variable name between the last quote mark and closing parenthesis like so:

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

You don't need the "+" symbol as we are not concatenating strings here. Think of it as a substitution process. Java will substitute the variable listed at the end, firstName in this case, for the string formatter %s.

As you work through the course you will learn more about the string formatter, so if this still doesn't make sense ask questions now so you have a good foundation on which to build.

Ken

Ken Alger Craig Dennis Thank you for the responses, it makes more sense now. I didn't realize I needed to put the variable at the end, it wasn't clear to me what needed to be done. Thanks for letting me know that the "+" isn't necessary for what we're doing right now.

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Melissa;

You need to tell the console.printf function what to replace the string formatter, %s, with. An example would be:

String animal = "dog";
console.printf("My %s ate my homework.", animal);

Post back if you still get stuck with the challenge.

Happy coding, Ken

I tried replacing my code with code similar to yours and putting the similar code beneath my original code, both times it gave me an error saying either Task 1 or Task 2 is no longer passing.

String animal = "Rats";
console.printf("%s + make good pets");

String firstName = "Melissa";
console.printf("%s + can code in Java");
String animal = "Rats";
console.printf("%s + make good pets");
Craig Dennis
Craig Dennis
Treehouse Teacher

Melissa the + is needed for String concatenation, which we haven't covered yet. String formatting just replaces the %s with the variables you add as arguments after the first string formatter:

String seeThisVariableHere = "Hi";
console.printf("%s Melissa!", seeThisVariableHere);  // Note the comma and reuse.