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 Data Structures - Retired Exploring the Java Collection Framework Maps

What is the problem with "println" when using Maps?

So as I work along with Craig, I got to the point where we print how many times has each hash tag been used. He used the "printf" command and I figured he'll be using the %n so I went ahead and used "println" instead so I wouldn't write the special symbol (not that I care, I just thought I'll do it that way). And boom and error - said that "no suitable method found for println(String,Map<String,Integer>)"

So I changed it back to "printf" and the %n and it worked just fine. So I was wondering what's the problem and which of the two commands is used more and "is better".

Would appreciate some input Craig Dennis

4 Answers

jrabello
jrabello
17,917 Points

I have no strong java background but I can tell you something about printf

printf function comes originally from C language, and has the following prototype:

int printf ( const char * format, ... ); 

That means that printf expects as the first argument an string that is parsed, so you know how many arguments to expect, example:

printf("%s %s",myString,myString2);
printf("%s %s %d",myString,myString2,mySingedInteger);
Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

As jrabello stated, println doesn't allow substitution as it just takes one parameter. Use printf when you are printing formatted text.

Make sense?

Thanks you both for the answers. I understand it now :smile:

Michael Acosta Pegoraro
Michael Acosta Pegoraro
4,911 Points

This means that printf cannot take methods/functions inside the parameter and println can right? or I'm wrong?