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

isaias santiago
isaias santiago
2,460 Points

help with Call the printf method on the console object and make it write out "<YOUR NAME> can code in Java!"

stuck

Name.java
// I have setup a java.io.Console object for you named console
String firstName="isaias";
console.printf("isaias");

3 Answers

Well, first you have to put it in as a string as in %s. Plus, its asking you to put in <Your Name> can code in java, not just <Your Name>. Hope this helps.

Matthew Hardy
Matthew Hardy
4,063 Points

Using the printf method allows you to print out a formatted string, this means you can input variables within the string amongst other, definite words or characters. In short this means you can say different things based on the value of a variable. To properly use the printf method or any formatted string, you write what you want the printed string to be but for any place where you want to enter in a variable you utilize a variation of %(letter) based on the type of variable inputted. For example, if you want to say "I have 5 apples" where the number five is variable you would use

console.printf("I have %d apples", nameOfYourVariable);

For this example your variable is a String named firstName, and you want the output to be "<YOUR NAME> can code in Java!" To accomplish this you must pass the variable firstName into a String with "can code in Java!" pre-determined. To accomplish this you would type: (%s is what you use to pass in Strings)

console.printf("%s can code in Java!", firstName);

thnks this help me...