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

Did i use the incorrect syntax...?

I do not understand what I did wrong here.

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

1 Answer

John Anselmo
PLUS
John Anselmo
Courses Plus Student 2,281 Points

Try to go back and watch the video one more time! console.readLine is not meant to store assigned strings. It's supposed to take in user input, for instance, typing:

String gender = console.readLine("What is your gender? ");

and then running it, will open the console, print What is your gender?, and then it will wait at What is your gender?. It is waiting for your input. So if you type in male, then the variable gender will be set to male, since you entered male into What is your gender?.

In this case, for task 1/4 you want your code to look like this:

String firstName = console.readLine("First name: ");

For task 2/4 your code should look like this:

String firstName = console.readLine("First name: ");
String lastName = console.readLine("Last name: ");

For task 3/4 your code should look like this:

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

Explanation: The challenge states that they want you to use printf to display "First name: ", followed by the first name entered by the user. You used System.out.printf, which would typically work, but in this case it doesn't want you to use it, it wants you to use console.printf. Next, you entered ("First name: ", firstName); which is so close! You just forgot the string formatter: %s.

For task 4/4 repeat task 3/4 except for variable lastName:

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

Hope this helped!