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

Where I need to place "%s" ?

.

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");
console.printf("firstName:");

1 Answer

Hi Martin,

I covered this in my other answer but I thought I'd answer this too for completeness.

Focussing just on the output string here, the printf - the rest is answered elsewhere.

The printf method outputs a string. That string can be contained within double-quotes, as you have done. Anything within double quotes is just a string; it cannot access variable values. So you've got "firstName" - that's not the variable, that's just a string that says "firstName".

To access the value of a variable within a string, you can use % characters - there are % characters for numbers, strings etc. You'll cover it all in the courses. You insert the %s, for a string insertion, where you want the string to be inserted. Then, after the double quotes (and a comma) you type the variable name:

String firstName = "Steve" // I didn't ask for user input
console.printf("First name: %s", firstName); // Value Steve gets inserted where the %s is

I hope that helps,

Steve.