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 Getting Started with Java IO

I need help, I need to know what i am doing wrong it is asking for %s?

Need help with one of the challenges

IO.java
String firstName = console.readLine("What is your first name?  ");
String lastName = console.readLine("What is your last name?  ");
console.printf("First name:", firstName);

2 Answers

You're supposed to place a %s at the last line to give a reference for where you are going to input the variable. The statement for printing out was also incorrect. It's like this:

System.out.printf("Firstname: %s", firstName);

For this Code Challenge System.out has already been imported and placed into a variable named "console". This is from the challenge:

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

Hello Leong, Seems like you forgot something, the %s. That is why it is prompting you for it. The printf() method takes in an argument, which you have by the way of "firstName". Because firstName is of String type you need to tell printf() where in the output string you want it to be placed with the %s. Your code should look like this:

String firstName = console.readLine("What is your first name?  ");
String lastName = console.readLine("What is your last name?  ");

console.printf("First name: %s %n", firstName);

I have also added the %n to tell the console to move to a new line after it finishes printing the text. Otherwise your prompt will be on the same line after the firstName.

Hope this helps and good luck!