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 trialStayl T
968 PointsError 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
jcorum
71,830 Pointsprintf() 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.
Suraj Ahuja
1,318 Pointstry writing System.out.println("The characters name is "+dispenser.mCharacterName); println uses concat to attach values.Hope it helps
Stayl T
968 PointsThat really works! Thanks!
osman musse
100 PointsPrintln 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
osman musse
100 Pointsso use print.out.printf instead of print.out.ln
Stayl T
968 PointsStayl T
968 PointsThanks a lot!