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

how do i write expressions using a string formatter?

how do i write expressions using a string formatter?

Name.java
// I have setup a java.io.Console object for you named console
String firstName="joseph";
System.out.printf("<YOUR NAME> can code in Java!");
System.out.printf("can code in Java firstName! %s");

1 Answer

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Hi Joseph, Welcome to Java :)

I see you've used the original strings as provided by the code challenge.

What you have to do is replace those with a "format specifier" and then pass it in as a comma separated value to printf().

So you would try something like this,

String userName = "josephsaika";
printf("Welcome to the website, %s", userName)

You can pass in multiple format specifiers to print. Passing in multiple variables to printf() and changing the order determines where they appear in the output.

Have a go and let me know if you need further help :)

... thanks will come back to you shortly

i have written it like this System.out.printf("can code in Java! can code in Java firstName! ,%s",() joseph);

its indicating an error on the letter j of joseph

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

Because in your example you're trying to link the format specifier to the String firstName =` variable that you declared. That will then mean that string joseph gets output to the screen.

Have a look at my example:

  • Declare the string and assign it its value
  • And then look at where I put my format specifier and then the reference to the String variable.