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

Lucas Santos
19,315 PointsWhat is Console.console = System.console();
Here is prompter.java
import java.io.Console;
//class
public class Prompter {
private Game mGame;
//constructor
public Prompter(Game game){
mGame = game;
}
public boolean promptForGuess(){
Console.console = System.console();
String guessAsString - console.readLine("Enter a letter: ");
char guess = guessAsString.charAt(0);
return mGame.applyGuess(guess);
}
}
I do not understand:
Console.console = System.console();
I know that it's part of the java.io.Console package but why do we need it? please explain how this code works and what it allows us to do.
2 Answers

mrben
8,068 PointsThe line should be:
Console console = System.console();
After doing this the console
object with give you a way to interact with the console/terminal from where you ran the program. For example, taking input from the user or writing things out to the screen.
You use it in the next line (where you have a typo, btw, it should be):
String guessAsString = console.readLine("Enter a letter: ");
Here you prompt the user with a message "Enter a letter:" and read in, from the console, whatever they type, and store it in the String
variable guessAsString
for later use.

Steve Kinsey
4,463 PointsInteresting info about the System.console and explains why I couldn't get it working on a Java program I wrote using JCreator (I eventually used Scanner instead, which worked fine...)
Thanks mrben
Lucas Santos
19,315 PointsLucas Santos
19,315 PointsSo what you are saying is if I did not have this code:
Console console = System.console();
Then I would not be able to use
console.readLine()
because it would not recognize console there for not recognizing readLine() method of the Console class?mrben
8,068 Pointsmrben
8,068 PointsYeah precisely.
Lucas Santos
19,315 PointsLucas Santos
19,315 PointsI see thanks! Seems to me that they should already automatically include the console object.
mrben
8,068 Pointsmrben
8,068 PointsNo problem. To explain a little further, just in case it's useful ...
There's the Console type and that has all the methods you need.
Then there's the handy
System.console()
method that "Returns the unique Console object associated with the current Java virtual machine, if any".So perhaps in theory you could be working with a console object that is not local but on a remote computer but in this program you're using the local system console.