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

please explain me the following question and also send me the code for it..

Using the console's printf method, display a message that says, "Last name: " followed by the last name that the user has entered.

IO.java
// I have imported java.io.Console for you.  It is a variable called console.


console.readLine("%s likes water\n",  firstName); // firstName doesn't exist yet
String lastName = console.readLine("Lourido"); // prompt for the user's input
String firstName = console.readLine("What is your last name? %s",  lastName); // don't use a placeholder; just ask the question
console.printf("What is your first name sir? %s",  firstName); // output, not prompt - format = First name: <firstName>
console.printf("What is your last name sir? %s",  lastName); // as above

i copied the above code but it is showing error

2 Answers

Hi there,

You've got a couple of issues here - but you're not far off.

Your first line of code isn't necessary and it also uses firstName which doesn't yet exist in your code. Best just delete that line for now.

You then want to gather the user's input twice. First, prompt the user to enter their first name and use console.readLine to prompt for the user's first name, and store that in String firstName using the equals operator. You've pretty much got that, you just need to tinker with your prompt. Repeat this for lastName in the same way on the second line - so gather the first name first, then move to the last name.

Next, you're asked to use console.printf to output the user's answers. The question gives a specific format for that output. It should look like First name: **, then the contents of firstName (use %s as you have done), then do the same using the lastName variable with the string **Last name: %s.

I hope that helps you out. I added some comments in your code to try to further explain the above.

Steve.