Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Basics Using your New Tools Multiple Format Strings

Noun and Adj - please help me. I ain't got no clue why the code isn't working. Thank you, colleagues!

Noun and Adj - please help me. I ain't got no clue why the code isn't working. Thank you, colleagues! Have a great time learning how to code! :)

// I've imported java.io.Console for you.  It is stored in a variable called console for you.

String name = console.readLine("It's smart coding in Java");
String pastTenseVerb = console.readLine("Grammar");

console.printf(" %s really %s this coding exercise.");

String noun= "It's smart coding in Java"; 
String adj= "Grammar";

Imma stick to my "noun" and "adj" called so :D

2 Answers

9,835 Points

Hi Raluca!

When using printf with %s in the text/format, you need to include a String variable.

In this example

public void example() {
    String myFirstName = "Raluca";
    console.printf("Hi, my name is %s. Nice to meet you!", myFirstName);

Output: "Hi, my name is Raluca. Nice to meet you!"

Notice I have added a %s inside the double quotations of the printf. The Java compiler now expects me to add a String variable as an argument after the double quotations, in this case firstName (or whatever other String variable you might choose).

The syntax for printf is

printf(format, arguments);

Format is any String (Either from a variable or in double quotations) and arguments are variables that the Java compiler expects if you have added a %s. Also notice the comma (,) following format in printf() as it's used to separate the two.

EDIT: If you have several %s in your format text, you can add more arguments by separating them with commas

console.printf("format %s %s", argumentOne, argumentTwo);

Hello and thank you very much for your detailed answer, saykin! :) It's so cool when I get this kind of help. I really appreciate it and wish you all the best! Have a great day!