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 am ask to do exactly by saying "Now replace your name with the FirstName variable using a string formatter"

I seem not to understand what the question is asking of me.

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

1 Answer

Stone Preston
Stone Preston
42,016 Points

generally a printf statement looks like this:

console.printf("this is is %s a printf", someVariable);

note the variable goes at the end of the statement

the value of someVariable will be inserted into the string where the format specifier is (it begins with a %). there are different format specifiers and you have to use the one that matches the type of the variable you are printing.

you can insert more than one variable and the format specifiers can go anywhere inside the quotes:

console.printf("%s is %s awesome", myName, someAdjective);

since myName is listed first, its value will be inserted where the first %s. since someAdjective is listed second, its value will be inserted where the second %s is.

in this case, you need to print "<YOUR NAME> can code in Java!" but insert the value of the firstName variable where it says <YOUR NAME>. you can do this like so:

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

since firstName is a string, we use the %s format specifier.

for a list of the format specifiers see this article