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

Faraz Malik
Faraz Malik
8,232 Points

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);
Eric Queen
Eric Queen
11,995 Points

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.
Eric Queen
Eric Queen
11,995 Points

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!