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

Hey! :) What I did Wrong?

?

IO.java
// I have imported java.io.Console for you.  It is a variable called console.
String firstName = console.readLine("Martin");
String lastName = console.readLine("Geil");
String FirstName = console.printf ("%s");

2 Answers

Hi Martin,

You're almost there:

// I have imported java.io.Console for you.  It is a variable called console.
String firstName = console.readLine("What is your first name?: ");  // user answers here
String lastName = console.readLine("What is your last name?: ");
console.printf("First name: %s", firstName);   // value of firstName goes where the %s is
console.printf("Last name: %s", lastName);

So, you declare the String objects first, and assign the values into them using user input, that's what the reaLine() method does. The user answers the question, and that answer gets stored in the variables, firstName and lastName.

We then output those strings using a formatted printf. For that, we put the %s where we want the variable value to go. So the output string First name: %s will be followed with the firstName variable and the value of that variable will be inserted where the %s is. Have a look at the code to see what I mean.

I hope that helps you out.

Steve.

ooou... thanks a lot :)

I just added some more in-depth commentary to my initial answer. Let me know if you still have any questions.

Steve.

Thanks, good explonation

No problem - happy to help.

Steve.