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

Daniela Risse
Daniela Risse
1,040 Points

Is this use of printf correct (firstName declared in advance): console.printf(firstName, "can code in Java!");

String firstName = "Daniela"; console.printf(firstName, "can code in Java!");

I get the error that I should check for the spelling of my content of printf, but I have no idea what I am doing wrong.

Thank you in advance!

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

3 Answers

Sorry about that pretty late and I wasn't paying attention. You're close but slightly off what you're actually looking for is

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

You want to have the %s inside the quotation so the console can read it as part of the code then explain what should be used there by adding it after the string",

Daniela Risse
Daniela Risse
1,040 Points

Thank you, but this:

String firstName ="Daniela"; console.printf(firstName + "can code in Java!");

does not work either.

Now I have tried the following:

String firstName="Daniela";

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

but this does not work either. Looks like I am not doing too well in this class.

Christopher Augg
Christopher Augg
21,223 Points

Daniela,

Sorry about the confusion. Please allow me to clear some things up.

Your first code will actually compile

       console.printf(firstName + "can code in Java!");

and print out:

Danielacan code in Java! 

However, even if you put a space where needed, the challenge is asking you to use a console.printf expression with the firstName variable using the string formatter. It is the %s that kheiferfuller showed how to use in his answer post.

Your second code:

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

will only print out:

Daniela                                                                               

This is due to the implementation of the method. It only takes these parameters:

  • format - A format string as described in Format string syntax.

  • args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification. The behaviour on a null argument depends on the conversion. Source: Oracle

You are doing just fine in the class. Programming frustrates us all at some point or another. There will be times when it is easy and times when you are pulling out your hair for a day or more trying to figure something out only to find that you had your curly braces wrapping the wrong sections of code. Just remember, if the things we are trying to learn are hard, then we are learning. When those things are easy, we already know them.

Regards,

Chris

Daniela Risse
Daniela Risse
1,040 Points

Thank you very much, kheiferfuller and Chris! This has helped me a lot and clarified a couple of things for me :-D