Java Scanner common method

Scanner Description:

Java 5 added java.util.Scanner class, which is a new utility for scanning the input text. It is some combination between previous and StringTokenizer Matcher class. Since any data to be retrieved by capturing the same pattern group, or by using an index to retrieve the sections of text. It can then be used in conjunction with a regular expression and method to retrieve a particular type of data item from the input stream. Thus, in addition to the use of regular expressions outside, it can also be arbitrarily Scanner class string and basic types (such as int and double) to analyze the data. By means of the Scanner, you can write a custom parser for any text to be processed.

Scanner use :( program is running, how to accept the value of the user's keyboard input)

The first step: Import Scanner class

import java.util. *;

Step 2: Create Object Scanner

Scanner input=new Scanner(System.in);

The third step: get keyboard input data

int now=input.nextlnt();

The difference next () and nextLine () of

int-type variable receiving the nextInt INPUT ();. : // approximation to such other types. nextLine (): read the input, including spaces between words and the symbols other than the transport of all (i.e., it reads the line.). After reading the inputs, nextLine () to locate the cursor on the next line.

receiving a string variable input.next (); : // Next (): read only until the input space. It can not read the two separated by spaces or symbols words. Further, next () after reading the input cursor on the same line. (read-only data prior to the space next (), and the cursor is pointing to the Bank)

String char into receiving input.next ();

Code demonstrates:

image.png

operation result:

image.png

Other methods to wait for the next update.

There is no big brother the king, with open black duck

Guess you like

Origin blog.51cto.com/14475876/2439011