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 trialMichael Acosta Pegoraro
4,911 PointsQuestion: Console inside of methods for developers with C++ background.
Do you have to import the console on every method that is going to be using it?
on C++ you can use cout whenever you want without having to import the whole Console console = System.console() thingy on every method you are going to use once you import the package.
It would be a pain to do this all the time.
Do I really have to call this console line on every method I want it to use the console?
:(
2 Answers
Craig Lovell
5,752 PointsFirst off you should know that importing in Java is a little be different then including in c++. In Java its just so you don't have to type the fully qualified name, but in c++ its to copy the code to the code. So technically if you did not want to include the package you could type java.io.Console console = System.console(), but that is usually not held a good coding practice. Secondly most Java developers never would use the console class (since this is kinda just a wrapper class anyways). Most Java developers use the System.out and System.in objects for input and output. And you don't have to import them.
For example to print hello world I would do the following.
System.out.println("Hello World") // or printf, or whatever
To do input I usually use the Scanner class, but I have seen others use the BufferedReader class.
Scanner scan = new Scanner(System.in);
String answerTheQuestion = scan.nextLine(); // or next(), or whatever
Craig Lovell
5,752 PointsOh, also we never import on a method level in Java. Its always done before the class declaration.
Michael Acosta Pegoraro
4,911 PointsMichael Acosta Pegoraro
4,911 PointsSounds fair, alright, I will give it a shot in a few. Thanks Craig, I've seen other reply's from you and they are always very helpful. ;)
Craig Lovell
5,752 PointsCraig Lovell
5,752 PointsThanks for the kind words. :)
Also I forgot to mention this but the Console object won't work in an IDE. The reason it works on the right now is because you are running your code in the console with the javac and java commands. In short the Console needs to attach itself to a console, and even though an IDE console looks pretty much the same as a regular console its actually a little bit different. I have seen some goofy work arounds, that had something to do with subclassing the Console class in an attempt to get the Console class to clear some boolean check that it does at initiation but honestly I do not see that much of a benefit to using the Console class. If I were you, I would just get used to using System.in and System.out.