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

Andrei Oprescu
Andrei Oprescu
9,547 Points

Scanner scanner = new Scanner(System.in); and String guessInput = scanner.nextLine();

For some reason, I have looked everywhere to try to understand these lines and I still can't understand them. Can someone explain them to me ?

Thanks!

1 Answer

I'm no expert, so take my answer with a grain of salt, and please someone correct any missteps I may make .....

  • The first Scanner (with the capital S) is the name of the class being referenced (in this case, it's a built-in Java class).
  • The second scanner (lower-case s) is the variable used to contain this specific instance of the Scanner. (Given the potential confusion factor of Scanner scanner = new Scanner, sometimes people may use Scanner input = new Scanner, since this class is dealing with taking user input.)
  • = new Scanner(System.in) says that this is creating a new instance of the Scanner class, which requires the in method of the System class (another built-in Java class) ... meaning that it is going to take IN information
  • String guessInput declares a String variable named guessInput
  • = scanner.nextLine() runs the nextLine() method on the scanner variable, which says that the program is going to "scan" for the "next line" of user input, and assigns whatever String it takes "in" to the variable guessInput. (if you did the input in Scanner input = new Scanner(System.in) earlier, than this would be input.nextLine())