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

Malachi Diaz
Malachi Diaz
453 Points

How do I pass the `firstName` parameter to the printf function?

I'm in the learning Java program and I can't figure out how to print onto the screen

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

1 Answer

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

You are almost done:

With the readLine() method the programe stores the input from your user inside of firstName or lastName. You dont need to use firstName or lastName with the readLine() inside of the (" "). So You can code it like this

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

so if you code this

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

the console will print the String (firstName), the user typed in ....

In your code you already defined the firstName and lastName, so you can also print them using the code above :)

And I have made a little change from / to \ ... this should help :)

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

Shout out if i am wrong, i am also very new to programming :)