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 IO

Frank Coles
Frank Coles
102 Points

How can I use the console's printf method to display a message that says, "First name: ", followed by the first name?

Here is my code:

String firstName = console.readLine(Dude); String lastName = console.readLine(); console.printf("First name: ", firstName); //I need help with this line of code

IO.java
// I have imported java.io.Console for you.  It is a variable called console.
String firstName = console.readLine(Frank);
String lastName = console.readLine();
console.printf ("First name: ", firstName);

You may want to respond with an answer next time instead of a comment :)

Mike Papamichail
Mike Papamichail
4,883 Points

You are right, for some reason i hadn't noticed the Answer button until you mentioned it. Will do mate!

1 Answer

Mike Papamichail
Mike Papamichail
4,883 Points

Hey there, since you are using the .printf() method -which prints out a Formatted String- you need to use the %s symbol like so:

// I have imported java.io.Console for you.  It is a variable called console.
String firstName = console.readLine(Frank);
String lastName = console.readLine();
console.printf ("First name: %s", firstName);

The %s will be replaced by the String that you have passed in the method as a 2nd argument.

PS: You can use multiple %s symbols in order to print more advanced text messages.In your case you could print out the last name too:

// I have imported java.io.Console for you.  It is a variable called console.
String firstName = console.readLine(Frank);
String lastName = console.readLine();
console.printf ("First name: %s!\nLast name: %s.\n", firstName,lastName);