How the Java token Read method and scanner work

Source: Internet
Author: User

How the scanner works

Nextint,nextdouble,next and so on are all token-reading methods. Nextline is not a token-reading method.

How the token Read method works:

Skips any delimiters first, and then reads a token that ends with a delimiter . Then corresponding to the Nextbyte,nextint,nextlong method, the token is automatically converted to a byte,int,long, respectively.

Next,nextline all read a string

Next is the read delimiter split string nextline reads rows separated by row separators.

Row separators are system-defined. The WinDOS platform is \ r \ n, in Unix is \ n, in order to get the row separator under a particular platform

Used: String lineseparator = System.getproperty ("Line.separator");

The token read method cannot read the delimiter after the token. If you call nextline after the token-reading method, read from the delimiter, to the line delimiter. The line delimiter is also read but not part of the string.

Note that following next nextline will cause a few things to happen.

For example

Scanner input = new Scanner (system.in);

Int intvalue= Input.nextint ()

String line = Input.nextline ();


Input 34 123 Return car

Intvalue line is the space plus 123;

If input 34 return to the car 123 return car (actually enter 34 after press ENTER program is finished)

Intvalue or 34,line is an empty string.


How the Java token Read method and scanner work

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.