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?
// I have setup a 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
Jonathan Grieve
Treehouse Moderator 82,446 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 82,446 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.