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 trialGina LaFontaine
51 PointsHow do I add a string formatter to my console.printf code?
I am a beginning and I'm already stuck. Could use some advice on how to add a string formatter to my code.
// I have setup a java.io.Console object for you named console
String firstName = ("Gina");
console.printf("firstName can code in Java");
3 Answers
dmzstudio45
43,976 PointsYes, that's another way to do it. You can put in a % for each variable you want to insert, and then add the names of each variable at the end. Both methods will work. I noticed that you just started today, so I'd say you're doing quite well for your first day. :D
dmzstudio45
43,976 PointsThis link has a more detailed explanation of this topic.
https://teamtreehouse.com/community/difference-between-systemoutprintf-and-systemoutprintln
dmzstudio45
43,976 PointsHi Gina,
in your print statement, you want the value stored in the firstName variable to print, but right now you are just printing firstName instead of the value stored, which should be your name.
The way to make that work is using "concatenation".
For example, my name is David, so the value stored in firstName is David.
So: console.printf(firstName) will print: David.
Then, if I want to print the rest of the message, I have to combine the value David with the string " can code in Java!".
console.printf(firstName + " can code in Java!");
See what I did there? The "+" will combine the two different things together into one string.
Gina LaFontaine
51 PointsThanks so much David. I finally got it to work. The answer the quiz wanted from me was console.printf("%s can code in Java", firstName); And as I'm retyping this to you, my guess is I've made another syntax mistake. But whatever I did type thankfully worked and I was able to finish my first coding quiz. It's all clear as mud right now, but hopefully it will eventually start making sense. : )
Thanks again.