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 Strings, Variables, and Formatting

Nicholas Sabbato
Nicholas Sabbato
523 Points

What does "call the printf method on the console object" mean?

I have my String firstName = "Nick"; and now it is asking me to run my name. I can type it, but I don't know how to run what I typed out.

Name.java
// I have setup a java.io.Console object for you named console
String firstName = "Nick";
console.printf("%s\n can code in Java!, firstName");

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! It's not actually asking you to run the code. The challenge will run your code for you. You have two problems in your code. One is a syntax error and one is a formatting error. You've put a new line character after your name which is not required by the challenge and will likely cause it to fail. Let's take a look at a similar example:

String favorite_food = "tacos";
console.printf("My favorite food is %s", favorite_food);

Because you've placed your closed quotations in the incorrect place, it now believes that "firstName" is not a variable but rather a part of the string literal, And because it's got the %s token in the string literal, it's still looking for a variable name. You must first end the string literal containing the string token, then add a comma, and finally the name of the variable. The %s will now be replaced with the value located inside the variable referenced.

As far as calling the printf method on the console object, that part you've done just fine (barring the syntax error). Hope this helps! :sparkles:

You made your whole printf method a string. Here is how it should look, notice all I did was move the last quote to the end of the string (before the comma) and removed your new line \n.

// I have setup a java.io.Console object for you named console
String firstName = "Nick";
console.printf("%s can code in Java!", firstName); //<------------Edited this code