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

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,558 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);