11
Scanner input = new Scanner(System.in);

Could you give me a detailed explanation on what the code above is doing step by step? I don't really understand how it works and how it links to me later being able to do this statement:

int i = input.nextInt()
The Man
  • 139
  • 1
  • 1
  • 4
  • 3
    Do you know what a `class` means in Java? – moffeltje Jun 03 '15 at 06:52
  • 1
    http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html – almightyGOSU Jun 03 '15 at 06:52
  • @moffeltje I do know what class and objects mean in C++, though I haven't get to that part in Java. – The Man Jun 03 '15 at 07:03
  • Do you know what IO streams/readers/writers are? Do you know what is `System.in`? – Pshemo Jun 03 '15 at 07:09
  • @Pshemo It is an object that reads input from specifically the keyboard? – The Man Jun 03 '15 at 07:10
  • Yes, it is input standard input stream which is meant to read data from user (by default from keyboard). Now Scanner is class which is meant to read data from streams (or even readers) in textual form. So its instance needs to know which stream it needs to read. We pass this information via constructor just like in your case, since we want Scanner to read from standard input stream we are using `new Scanner(System.in);`. Then we store created instance in `input` reference. This lets us use methods like` nextInt()` `nextLine()` which are not available for `InputStream`. – Pshemo Jun 03 '15 at 07:16
  • @Pshemo Got it now. Thanks for helping me out! – The Man Jun 03 '15 at 07:30
  • 1
    BTW, as mentioned earlier Scanner can be used to read data as text from any InputStream or Reader. So it can also read from file if you pass instance of FileInputStream or FileReader, or any source which can be wrapped by any InputStream/Reader. – Pshemo Jun 03 '15 at 07:32

4 Answers4

11

Alright, let's elaborate with some simplified explanation about the Scanner class.

It is a standard Oracle class which you can use by calling the import java.util.Scanner.

So let's make a basic example of the class:

class Scanner{
   InputStream source;

   Scanner(InputStream src){
       this.source = src;
   }

   int nextInt(){
       int nextInteger;
       //Scans the next token of the input as an int from the source.
       return nextInteger;
   }
}

Now when you call Scanner input = new Scanner(System.in); you make a new object of the Scanner class (so you make a new "Scanner") and you store it in the variable input. At the same time you are calling the (so called) constructor of the class, with the parameter System.in. That means it is going to read from the standard input stream of the program.

Now when you are calling input.nextInt(); you execute the method from the object you just created (also documented). But as we see, this method returns a integer, so if we want to use that integer, we have to assign the call to a variable like you do:

int i = input.nextInt();
moffeltje
  • 4,095
  • 4
  • 24
  • 49
6

Scanner input = new Scanner(System.in); creates a new Scanner instance which points to the input stream passed as argument. In your case the steam is Standard input stream.

So, once your scanner instance is pointing to it, you can scan the stream and get integers, strings and do other stuff .

TheLostMind
  • 34,842
  • 11
  • 64
  • 97
0
Scanner input = new Scanner(System.in);

Creates a new object of type Scanner from the standard input of the program (in this case probably the console) and

int i = input.nextInt()

uses the nextInt Method of that object, which allows you to enter some text and it will be parsed into an integer.

Tim Kathete Stadler
  • 1,049
  • 3
  • 23
  • 47
0
Scanner s = new Scanner(System.in);

Above statement creates an object of Scanner class which is defined in java.util.scanner package. Scanner class allows user to take input from console.

System.in is passed as a parameter in Scanner class. It tells the java compiler that system input will be provided through console(keyboard).

Ardent Coder
  • 3,309
  • 9
  • 18
  • 39