Scanner for Java - read data from console

Introduction to the Scanner class

The Scanner class, a new utility for scanning input text. Since any data must be retrieved by capturing groups of the same pattern or by using an index to retrieve parts of the text. It is then possible to combine regular expressions with methods for retrieving specific types of data items from the input stream. In this way, in addition to using regular expressions, the Scanner class can arbitrarily analyze data of strings and basic types (such as int and double). With Scanner, you can write custom parsers for any text content you want to process.

Introduction to the methods of the Scanner class

useDelimiter :

public Scanner useDelimiter(Pattern pattern)

Set this scanner's delimitation mode to the specified mode.

next & hasNext:

public String next()

Find and return the next complete token from this scanner. The complete token is preceded and followed by input information that matches the delimited pattern. This method may block while waiting for input to scan, even if a previous call has hasNext()returned .true

public boolean hasNext()

Returns true if there is another token in this scanner's input. This method may block while waiting for input to scan. The scanner will not perform any input.

hasNextLine & nextLine:

public boolean hasNextLine()

Returns true if another row exists in this scanner's input. This method may block while waiting for input. The scanner does not perform any input.

public String nextLine()

This scanner executes the current line and returns skipped input information. This method returns the rest of the current line, excluding the line separator at the end. Moves the current position to the beginning of the next line.

Because this method continues to look for line separators in the input information, if there are no line separators, it may buffer all input information and look for lines to skip.

loading.......

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325472014&siteId=291194637