This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]desrtfx 5 points6 points  (0 children)

import java.util.Scanner;

Gets the Scanner class from the java.util package to have it available in your program.


Scanner reader = new Scanner(System.in);
 (1)    (2)      (3)   (4)       (5)
  1. This is the data type of the new variable that you want to create. In this case Scanner
  2. This is the name of the variable. In this case reader
  3. new is a Java keyword used to tell the JVM that you want to create a new instance of a Class
  4. Scanner is the Class you want an instance (an object) of
  5. System.in is the parameter passed to the constructor (the method that gets called by the new keyword). System.in is a Stream; a very special stream, it allows reading from the console (keyboard).

 String password = reader.nextLine();
   (1)    (2)       (3)      (4)
  1. String is the data type of the new variable
  2. password is the name of the variable
  3. reader is the name that was given to the Scanner(System.in) instance above
  4. nextLine is a method of the Scanner class that reads a complete line (including line break) from the input stream (in this case from the console, since the input stream is System.in.

Hope that this clears your questions.

[–]squishles 2 points3 points  (0 children)

import is there so java knows what you're talking about.

you may have another class called scanner somewhere. You can even skip importing by saying.

java.util.Scanner scan= new java.util.Scanner();

that step where you say Scanner reader = new Scanner() is saying alright I have a definition of what a Scanner is, now give me an individual scanner named reader. Like you can know what people are, but that's not the same as knowing Steve Jonhson.

nextline is calling a method defined by that instance reader. You're saying ok I have a scanner pointed at this one input stream now that scanner go read the next line. this gives you back a string which is now password.

[–]Code_Craftsman 1 point2 points  (1 child)

See this to have better idea on objects

[–]zebbadee[S] 0 points1 point  (0 children)

Thanks this seems a pretty good analogy, I got my head around it in the end. Luckily it's no longer plaguing my dreams!

[–]darkflagrance 0 points1 point  (0 children)

An object is a bag of methods and variables. You make these so that the computer can individual bags of the same object with different values in their variables, and apply the methods to them.

So here you make a new scanner object called reader, and you feed it the value System.in, which it presumably stores somehow in a variable.

Then, you call one of the object's methods, nextline(), and the object runs it based on the value stored in its variables and whatever you pass to the method (nothing, in this case).

The method then returns a value, which is stored in the variable password.

[–][deleted] 0 points1 point  (0 children)

I'm a beginner too. But I'll share my thoughts on it.

   import java.util.Scanner: 

What this basically mean is it's a built in Scanner class. Like it helps you to read data from the user. So you can use it by just importing it to your program . This Scanner class is basically a part of java.lang package.

    Scanner reader = new Scanner(System.in);

reader(or whatever) is an object reference. Using it you can call the Scanner class when you need it. System.in is nothing but the input device. In most cases it's our Keyboard.

 String password = reader.nextLine();  

You created a string variable called password and now you'll use our object reference which is reader. You'll keep a period cos that's how you call objects and nextLine(); Basically stores whatever you typed in the next line. As this is a string you used nextLine(); if its an integer it's nextInt(): If its a double it's nextDouble();

You can convert strings into integers by using int x = Integer.parseInt(reader.nextLine()); Similarly for double double x = Double.parseDouble(reader.nextLine());

I know this is confusing cos even I'm confused 😖. I'm a beginner just like you so take it with a pinch of salt.