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

Schwartz Prince
Schwartz Prince
Courses Plus Student 2,132 Points

Couldn't get the output. Please help

Please help me in finding out when I went wrong.

// I've imported java.io.Console for you.  It is stored in a variable called console for you.
String name = console.readLine("Schwartz");
String pastTenseVerb = console.readLine("Did ");
console.printf("%s", name, "really %s",pastTenseVerb, "this coding exercise");

2 Answers

28,521 Points

The problem is your printf method call. When using printf you have to pass a string to be formatted as the first argument (which can contain any number of placeholders) and then you provide the values for those placeholders as the further arguments. For example:

String example1 = "string1";
String example2 = "string2";
console.printf("The contents of example1 is %s, the contents of example2 is %s", example1, example2);
// The above will print: "The contents of example1 is string1, the contents of example2 is string2, example1, example2"

You cannot alternate between providing strings with placeholders and variables to place in them like you have done.

Joseph Wasden
Joseph Wasden
20,406 Points

You are close. So, first, with the field declarations. Whenever we use console.readLine(), we need to remember that the bit that is in the paranthesis (in other words, the parameters) are used as a text prompt to have the user type in a value. That typed value becomes the value of our variable. So, we want to keep the string in the parameters of the readLine method descriptive of the variable the user will be setting.

Also, when using printf(), you need to declare your entire string upfront. we can use the %s placeholder multiple times, and the comma separated bits after the string will be used concurrent to the order of the %s placeholders.

See the code below.

// I've imported java.io.Console for you.  It is stored in a variable called console for you.
String name = console.readLine("Enter a name:  ");
String pastTenseVerb = console.readLine("Enter a past-tense verb:  ");

console.printf("%s really %s this coding excercise", name, pastTenseVerb);