0
  Scanner scanner = new Scanner(System.in);

    System.out.println("enter number :");
    int number = scanner.nextInt();
    System.out.println("Number is " + number);

let me go through step by step to explain my understanding,

Scanner scanner = new Scanner(System.in);  
  • The Scanner is one of the java In-built classes and it is one of the ways we can use it to read user input.

  • scanner is the identifier of the object we created.

  • Using new, we create an instance of the object.

  • System.in helps us to take the inputs from the console(keyboard)

    System.out.println("enter number :");

  • prints the " enter number : ".

    int number = scanner.nextInt();

  • Reads the input given to the system.in from console(keyboard) and parse(convert) the input to an int and stores it in the variable number.

    System.out.println("Number is " + number);

  • prints the statement "Number is " + number(input stored in the variable number));

this is my vague understanding I think and I am not certain of my knowledge.

my questions are;

  1. Scanner class is used to read user input, then System.in also is used to read input? isn't it? it will be a help to explain the clear distinction between these two terms?

  2. when we type(enter) input from the console, where is it going? to the System.in?

does System.in storing the values?

  1. nextInt() method is also used to read inputs? how it works? is it read input that is already been read by the System.in and then parses to an int?

The scanner reads, System.in reads, nextInt() reads, everything reads? I can't able to distinguish the workings of these?

Jonas
  • 121,568
  • 97
  • 310
  • 388
  • `System.in` represents the console, `Scanner` represents something that can be used to read inputs, and `nextInt()` is just a way to actually make the application stop and wait to get an input. The only active thing here is the `nextInt()` call that triggers all of this. – sorifiend Apr 16 '21 at 04:35

2 Answers2

1

System.in is read from, it doesn't read itself.

The Scanner class helps parse input that it gets from the input source you give it. In this case, you've given it System.in, so it reads input from there.

nextInt() is one of the methods Scanner provides to help you parse input. Specifically, it parses an integer.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
1

System.in is an InputStream that (by default) provides user input from the console. You can read from it directly by using read:

int someByte = System.in.read();

Note that this reads an unsigned byte (Java doesn't have a type for unsigned bytes, so the type is int). For example, if I entered 1 in my console, the above will store 49 into someByte, because 49 is the first byte that the character 1 is encoded as.

If you want to someByte to be 1 instead, using System.in.read is clearly not very helpful, is it? We don't want to just read the bytes. We want to read the bytes, and use a particular encoding to convert those bytes to characters and then parse the string of characters to a number. This is what Scanner does.

A Scanner can take an input stream, and read bytes from it on demand, understands them as characters using a default encoding, then it groups chunks of characters together into tokens. One way for a Scanner to read stuff is by calling nextInt. What nextInt does is: read bytes until it finds a complete token, and parses it to an integer.

In short, System.in gives you the raw bytes, Scanner gives you tokens, parsed or otherwise. Hopefully that answers your first and third question.

when we type(enter) input from the console, where is it going? to the System.in?

Yes, at this level of abstraction, you can say that's the first place it will go. Then the bytes you inputted then get passed to Scanner, which then transforms them into tokens, and then parsed.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • I learned new things from your answer previously I am not aware of,1) so the input we enter from the keyword is read first by the System.in as bytes? 2) Then from System.in, the Scanner converts the bytes into characters(is it char data type ?) and then into tokens(tokens are texts right? so is it strings), – mockingbird Apr 17 '21 at 02:41
  • 3) nextInt() then reads the inputs from the Scanner, the input from the Scanner is tokens and what nextInt90 does is, parse the tokens into an integer? – mockingbird Apr 17 '21 at 02:45
  • @mockingbird 1) yes 2) Yes, if you look really deep down there, there will be `char`s. And yes, tokens are strings. 3) You seem to think the scanner and `nextInt` are separate things. No. `nextInt` is a thing that you can ask a scanner to do - "hey, scanner, read the next token, and parse it to an int". **The scanner doesn't read anything unless you tell to `nextInt`, or `next`, or `nextFloat`, etc** – Sweeper Apr 17 '21 at 02:55
  • Yeah yeah now I understand, Scanner won't do anything until invoking methods like next() or nextInt(), thanks man really... – mockingbird Apr 17 '21 at 03:18
  • Understanding System.in, is this right 1) From System.in, the Scanner gets its input to read. 2) System.in gets the input directly from the console(keyboard) as byte data type and transfer to the Scanner to read, parse, and so on. 3) so System.in don't read anything, it's just there to receive input from the console(keyboard) and transfer it back to the Scanner? if I am wrong, help me understand it – mockingbird Apr 17 '21 at 06:48
  • @mockingbird As far as Java is concerned, we say that `System.in` _is_ the standard stream of input from which you can get bytes from. The console is in no way "directly" connected to it. Your OS/the JVM decides that the standard input stream should be the console. `System.in` also doesn't transfer bytes to `Scanner` - `Scanner` _asks_ for bytes (again very indirectly). – Sweeper Apr 17 '21 at 07:02
  • So, System.in don't transfer the input it received from the console to the Scanner, the Scanner takes the input from the System.in? 2) what do you mean by Scanner asks for bytes? – mockingbird Apr 17 '21 at 07:16
  • @mockingbird Look, this starts to feel like a discussion, which is not what Stack Overflow, or the comment section is for. I'm also not your personal tutor. Frankly, I'm not sure if you have thoroughly read my answer. I would recommend that you do some exploration yourself. You can start from [`Scanner.readInput`](https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/util/Scanner.java#L838), which `nextInt` calls. – Sweeper Apr 17 '21 at 07:18
  • Yeah, your right i started pestering you, sorry for that... i am trying to understand, i just get doubts after reading everything you posted – mockingbird Apr 17 '21 at 07:26