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 Objects Creating the MVP Prompting for Guesses

Scanner... x.x

This whole scanner block of code that he just typed went right over my head.
Scanner scanner = new Scanner(System.in) for starters., ClassScanner is instantiating scanner to be the link to all the methods found in System.in Class Scanner?

2 Answers

Anders Björkland
Anders Björkland
7,481 Points

I will give this a shot. A Scanner is a type of object that can read a string of characters from a stream. A stream can be a string of characters from for example a text document or, as in this case, your keyboard. but how would the Scanner object know where to look? Well, you tell it in its constructor: Scanner scanner = new Scanner(System.in);

OK, so you told the Scanner object that its reading material is in System.in. So what is that? The .in shows us that we are accessing a public field (a variable) of the System class. What kind of field? Well, a stream. An InputStream. An InputStream that so happens to be connected to the keyboard.

So what you have now is an object that can read input from your keyboard by calling its methods next(), nextLine(), etc.

it clicked when you pieced together System.in to keyboard. thanks a lot!!