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 and Variables

Is 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

I'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); 

Hahaha! 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?

Not a problem, man. Yes, except that Oracle recommends using %n over \n, though, because %n is a platform independent format. Happy Thanksgiving!

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hi 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!

Thanks 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.

Hi 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