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 trialBarry Lares
467 PointsIs console.printf() good practice?
Love the teaching style so far, but quick question, In class were using System.out.println() using JGRASP as the IDE. console.printf() is not even talked about but i figured its because were using the workspace. However %s formatter is not working using System.out.println() in JGRASP . Any suggestions?
System.out.println(" %s is learning java", firstName); ^ method PrintStream.println() is not applicable (actual and formal argument lists differ in length) method PrintStream.println(boolean) is not applicable
3 Answers
Marcus Parsons
15,719 PointsI'd recommend using System.out.format which is also a part of the PrintStream library. It is equivalent to using printf and any type of format will work with that command.
For example,
System.out.format("The value of " + "the float variable is " +
"%f, while the value of the " + "integer variable is %d, " +
"and the string is %s", floatVar, intVar, stringVar);
Craig Dennis
Treehouse TeacherHi Barry Lares !
Thanks for the feedback!
The printf
method is on the PrintStream
object System.out
. java.io.Console
is just a wrapper class that attempts to simplify console applications. Printing is very similar, but reading can get quite nasty. java.io.Console
lets us focus on, in my opinion, more important things. I used it in an attempt to keep things clear before we cover what System.out
actually is.
So again, if you want to use formatters, use System.out.printf
.
Hope that helps!
Barry Lares
467 PointsThanks for the quick response Craig. I just joined Treehouse today actually and so far I really like how after the interactive lesson you get a quiz and a practical lesson. I'll be diving more into the course but so far so good. I like your teaching style and semantic coding. Proper coding practice early on is what creates average programmers from great ones. AND PRACTICE! :)
Thanks Craig.
ashwinjangam2
2,795 PointsHi Craig, I joined your JAVA and its wonderful, enjoyable, marvelous. Great Job you done for all students who really want tobe a programmer but I have a request could you please elaborate JAVA into Advance it will really help to be best programmer and some more interactive about how to create Apps
Thanks a lot
Cheers
Happiness grows when it share
Ashwin
Barry Lares
467 PointsBarry Lares
467 PointsHahaha! Thanks man. Problem Solved. So instead of using println, from now on practice using printf and use \n if i need to execute an addtional line. is that right?
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsNot a problem, man. Yes, except that Oracle recommends using %n over \n, though, because %n is a platform independent format. Happy Thanksgiving!