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 trialAndrei Oprescu
9,547 PointsScanner 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
Stephanie Youstra
18,513 PointsI'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 ofScanner scanner = new Scanner
, sometimes people may useScanner input = new Scanner
, since this class is dealing with taking user input.) -
= new Scanner(System.in)
says that this is creating anew
instance of theScanner
class, which requires thein
method of theSystem
class (another built-in Java class) ... meaning that it is going to take IN information -
String guessInput
declares aString
variable namedguessInput
-
= scanner.nextLine()
runs thenextLine()
method on thescanner
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 theinput
inScanner input = new Scanner(System.in)
earlier, than this would beinput.nextLine()
)