Java Java Basics Getting Started with Java IO

Using the console's printf method, display a message that says, "First name: ", followed by the first name that the user

confused what i am doing wrong

IO.java
// I have imported java.io.Console for you.  It is a variable called console.
String firstName = console.readLine ("kunal");
String lastName = console.readLine ("Grover");
Console.printf("%s\n,first name");

1 Answer

Heidi Fryzell
seal-mask
MOD
.a{fill-rule:evenodd;}techdegree seal-36
Heidi Fryzell
Front End Web Development Treehouse Moderator 20,612 Points

Hello Kunal,

You are very close.

The message that is printed to the console is the format string, it is the first parameter that is passed to printf. That string needs to output "First Name: Kunal". I think you might have gotten confused because you did not realize that "First Name:" needed to be part of the format string.

The variable, firstName, is the second parameter passed to printf, and it will replace the format specifier %s in the format string.

Notice how the 2 parameters passed to printf in parenthesis, the format string and the variable, are separated by commas.

Notice how the format string is surrounded by quotes but the variable, firstName is not.

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

This video goes over all these concepts: https://teamtreehouse.com/library/strings-and-variables

Happy Coding! Heidi

Heidi Fryzell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Heidi Fryzell
Front End Web Development Treehouse Moderator 20,612 Points

Hello again Kunal,

One more thing I noticed.

When you are calling the readLine method, the text in the quotations should be the question you are asking the user. You put your name in the quotations, but it is the user's answer that will get assigned to the string variable when they enter something into the prompt.

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

Hope that helps clarify. Heidi