Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects Delivering the MVP Validating and Normalizing User Input

Kate Woodroffe
Kate Woodroffe
Courses Plus Student 6,685 Points

I don't understand what the Scanner class is doing

There's a scanner class at the beginning of the promptForGuess method in Prompter.java. What does it do?

Joshua Thao
Joshua Thao
6,438 Points

Basically the scanner class is already built into Javas API or package, just like System.out.print but the Scanner class takes in the user's input. If you removed the scanner class you can't accept the user's input anymore.

1 Answer

Emmanuel C
Emmanuel C
10,635 Points

From Official Java Documentation

A simple text scanner which can parse primitive types and strings using regular expressions.

The Scanner class is useful for reading strings and other inputs like the console or files. Its most typically used to read input from the console, which is what System.in is. When you use scanner.nextLine() it reads everything that was entered on one line of the console and returns it as a string. In that case you can see it as the opposite of System.out.println(). where instead of writing a string to the console it reads it.

I hope that made sense, if not, feel free to leave a comment and ask questions. Good luck.