Home java java.util.Scanner, hasNext ... (); & amp; next ... (); Working with memory...

java.util.Scanner, hasNext … (); & amp; next … (); Working with memory object

Author

Date

Category

What’s going on ?! How do the class Scanner next methods … (); and hasNext … (); memory?

Example:

import java.util.Scanner;
public class Main {
  Public Static Void Main (String [] Args) {
    Scanner console = new Scanner (System.in);
    boolean b = console.hasNextLine ();
    String i = console.nextLine ();
    String str = console.nextLine ();
    System.out.println ( "Output:");
    System.out.println ( "i =" + i);
    System.out.println ( "str =" + str);
  }
}

When initializing the variable b will be launched from the console input. However, when initializing the variable i, it is not required. nextInt () method; somehow just takes the same value that was introduced just before the method call hasNextLine (); But already during the initialization variable str asks to enter a new line.

More fun:
Change the type of the variable i from String to int.
When initializing the variable b is activated by input from the console. The same input is forwarded to nextInt () method; when initializing the variable i. A variable initialization str generally overlooked! Do not ask to enter the next line.

import java.util.Scanner;
public class Main {
  Public Static Void Main (String [] Args) {
    Scanner console = new Scanner (System.in);
    boolean b = console.hasNextLine ();
    int i = console.nextInt ();
    String str = console.nextLine ();
    System.out.println ( "Output:");
    System.out.println ( "i =" + i);
    System.out.println ( "str =" + str);
  }
}

Obviously, this is due to the fact of how these methods with an object such as the Scanner memory, in this case the “console”. But where all these chips and cases itemized in detail – I do not know. How and what is going on in the second case ?!
Please help!


Answer 1

The first example

boolean b = console.hasNextLine ();

As the program to determine whether the next line will be introduced? No way. The input file can be closed at any time. Therefore, the program waits for data entry any day.

The input stream is buffered from the console by lines. Have you noticed that you can edit the line and the program does not see your actions until you click Enter ?

The combination of these two factors causes the program to wait until you enter the first line.

String i = console.nextLine ();

When this code is executed, the line has already been introduced and available programs. The variable i immediately filled with the value and we move on.

String str = console.nextLine ();

And the following line user has not introduced. The program stops and waits until you edit the text and press the Enter . After that, we move on.

Second Example

boolean b = console.hasNextLine ();

It’s all the same as in the example above.

int i = console.nextInt ();

where the line has been read, begin to dismantle it: missing whitespace, read the sign and the number of digits and the first stop on whitespace . This could be a line break, space or tab, but not only.

The number is read from the input stream character by character. When found improper symbol of his “return” in the flow, so that when you read on, you’ll see it first.

One way or another line break after reading the whole number will be in the flow.

String str = console.nextLine ();

We read the following line. But no, not the following: We read the rest of the current line. If you click ENTER Immediately after the number, you will get an empty string.

output

It can be seen that Nextint and NextLine somehow work well with each other. This is explained: Nextint It is arranged so as not to distinguish gaps from lines. NextLine – On the contrary.

Solve the problem can be radically: always read entire lines. If you do not need a string and an integer, then remove it from the line after reading:

string line = console.nextline ();
int i = integer.paraseint (Line, 10);

All mechanics are well described in official documentation on Scanner . Details related to buffering: The operating system is sent to enter the whole lines, etc.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions