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 Receiving Input

I have learned java before in my school but it's totally different, I didn't get it why we are using console & printf.

What I learned before in java is System.out.print method but not like this.

4 Answers

28,558 Points

It's just an alternative way of printing and getting input, when it comes to programming you'll often find that there are lots of slightly different ways to accomplish the same type of task.

The console class makes it easy to get input from the console, it is simpler than the other common alternative which is to use a Scanner object. The disadvantage though is that for various reasons too complicated to get into here the console class usually does not work when you run Java programs through an IDE. That is not a problem for Treehouse since they don't use IDEs for the basic courses, but it would be for any course which does.

If your school thought you Java and used some sort of Java IDE like IntelliJ or Eclipse then that would be the reason why they never used the console class.

And you can use printf without using the console class through the System.out.printf method. It works exactly the same as the console printf.

They do almost the same thing.

Just keep in mind when you use console.printf, you can format in stuff. For example, if you do this...

String name = "Alex";
console.printf("Hello there, %s! Nice to meet you!", name);

The output will be...

Hello there, Alex! Nice to meet you!

The %s in the string is like a placeholder where you can insert whatever you want. In this case, you insert name's value.

Remember: The string with the placeholder and the placeholder's value are separated by commas because the printf method take two of more arguments. printf is short for "print format [string]". There is also a println function in console that just prints whatever you tell it to print. (println stands for print line.)

I hope this helps. ~Alex

Alex One question, do you know how to run console.printf on Xcode ? I know this has nothing to do with treehouse, but I want to code in Xcode not on the treehouse's terminal.

Um... I think XCode is for iOS development (Swift, Objective-C, C), sadly...

If you want to code locally, you can follow along this course: Local Development Environments

umm! I own a mac and I don't know how to code local, you know. in my computer but thank tho!