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

I think I'm not good at Java, my first stammering is not going very well.

However, it will be a breeze for you: look at it

Name.java
// I have setup a java.io.Console object for you named console

String firstName = "Jo";
console.printf ( "Jo can code in Java");
//console.printf("Jo can code in java");
Simon Coates
Simon Coates
28,694 Points

While Alexander has the answer to this specific question, i'd note that once you're more established in Java, consulting the Java documentation can be helpful. (eg https://docs.oracle.com/javase/7/docs/api/java/io/Console.html). For now, it might be confusing and more effort than it's worth, but if you know what method you want to call (treehouse only expects you to use stuff it's shown you in tutorials or code), you can typically find out the number of parameters, whether a method returns anything and if there are overloaded versions of a method (different versions of a method that accept different types or numbers of parameters). Later on, the method documentation can give you a clue about if you need to write exception handling.

1 Answer

Hi,

You almost got it! the printf method however expects you to supply an argument. You want it to say "Jo can code in Java!" You already have a variable

String firstName = "Jo"

Now looking at the sentence you want it to say "Jo can code in Java!" you can replace "Jo" with firstName by using the printf method the following way.

String firstName = "Jo";
console.printf("%s can code in Java", firstName);

You can think of the "%s" as a placeholder for the String variable firstName. Any variable listed after the comma will replace a placeholder in the string you supply to the printf method. You could do something like this.

String firstName = "Alex";
Int age = 24;
console.printf("%s is %d years old", firstName, age);
//Which will print -> Alex is 24 years old

The "%d" is a placeholder for a double or integer. Check out the following cheat sheet http://alvinalexander.com/programming/printf-format-cheat-sheet