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 Objects (Retired) Meet Objects Privacy and Methods

Stayl T
Stayl T
968 Points

Error using System.out.println!

If I use println instead of printf in this line of code it shows error. Why?

System.out.println("The character's name is %s. ",dispenser.mCharacterName);

4 Answers

printf() expects to see one or more format specifiers like %s, and to replace them with variables. println() does not.

So in your code:

System.out.println("The character's name is %s. ",dispenser.mCharacterName);

println() doesn't know what to do with the comma and the variable after the comma.

Stayl T
Stayl T
968 Points

Thanks a lot!

Suraj Ahuja
Suraj Ahuja
1,318 Points

try writing System.out.println("The characters name is "+dispenser.mCharacterName); println uses concat to attach values.Hope it helps

Stayl T
Stayl T
968 Points

That really works! Thanks!

Println can not use %s because it is a formated string .Print.out.ln can only use normal strings like "osman" Buy Print.out.printf can use formated string like %s so cah

so use print.out.printf instead of print.out.ln