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

johnacosta
johnacosta
275 Points

%s

i was told to replace my name with the string formatter. I am assuming that it is %s. but I keep getting an error. Please help.

Name.java
// I have setup a java.io.Console object for you named console
String firstName = "John";
console.printf("%s");
console.printf("%s can code in Java");
James Stride
James Stride
Courses Plus Student 621 Points

When you use the string formatter. You have to put the string name after the end quote and a coma. So the compiler knows which variable you're talking about. Otherwise it comes up with nothing and therefore it doesnt know what you mean by %s. Hope this helps. Let me know. Have fun coding bro! For ex.
String firstName = "John"; console.printf(" %s can code in Java", firstName);

2 Answers

Steven Parker
Steven Parker
231,236 Points

For every format specifier (like "%s") in the format string, there should be an additional argument that supplies the value that will be rendered in that place.

So in this case, the specifier represents where your name will go, so you must supply the variable that contains your name as a second argument:

console.printf("%s can code in Java", firstName);
johnacosta
johnacosta
275 Points

Gotcha! Thanks! Its hard being new lol

Steven Parker
Steven Parker
231,236 Points

Keep plugging, you'll get there. Meanwhile, you know where to get to help now.

Happy coding!