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 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! :)

Multiple.java
// 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

saykin
saykin
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!