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

Jan Lundeen
Jan Lundeen
5,886 Points

Java basics-receiving input-challenge 4 of 4-Using console's printf method, display message Last name follow by input

On Java basics-receiving input-challenge 4 of 4-Using console's printf method, display message that says, "Last name: " followed by the last name that the user has entered, I continue to receive the following error:

Bummer: Ensure your format string contains the required text "First name:". The Output.html file is blank

Here's my code:

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

My String firstName variable appears to have "First name:" specified. I've been staring at this for a while and I can't find the answer. What is missing?

Thanks,

Jan

2 Answers

Hi Jan,

You want your output string to be "First name: Steve", where my name is held within firstName, so use string interpolation to insert %s into the string "First name: " as part of your printf output.

Make sense?

Steve.

Jan Lundeen
Jan Lundeen
5,886 Points

Hi Steve,

By output string, you're referring to the "String firstName = console.readLine"strings, right? Just to confirm my understanding, this line should read:

`String firstName = console.readLine("First Name: Jan"); '

I wanted to try it out on the challenge, but the Check Work button is not working, so I'm just confirming with you first.

Also, what do you mean by "string interpolation"? Does that mean defining the string first[e.g. String firstName = console.readLine("First Name: Jan");]. Next, you would represent it by using a shortcut for the string [e.g. %s] followed by the variable name to print it out?

Thanks,

Jan

Jan Lundeen
Jan Lundeen
5,886 Points

Hi Steven,

I figured it out(see the last two lines below). Looks like I didn't have to enter my name [e.g. String firstName = console.readLine("First Name: Jan"); Still not 100% sure whether I understand what you mean by string interpolation though. I think it's like using a variable (%s) and prompting the user to add the first and last name. Then, the name they enter will show up as the first or last name

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

Thanks,

Jan