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 Delivering the MVP Validating and Normalizing User Input

Kate Woodroffe
PLUS
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,439 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,636 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.