[To] Java: questions about the Usedelimiter () method of scanner

Source: Internet
Author: User

"From" https://segmentfault.com/q/1010000003885362

    1. Under Windows, we press the key on the keyboard Enter , actually entered is the carriage return and the newline two characters: \r\n , ASCII code respectively is13和10

    2. By default, the Scanner input you get is not included in the return line. For example, ____one_two\r\n if you type, it will only get the output from the ____one_two inside (of course, it may have to be obtained multiple times next() ), automatically filter the carriage return line

    3. But when we force a carriage return or change the behavior delimiter, it does not automatically filter the carriage return line break. For example, if you use a carriage return \n as a delimiter, enter the above content ____one_two\r , which is the reason for the return of length 12. You can use the following procedure to verify that you can find the last character of the string that gets to the ASCII code of 13, which indicates that it is a carriage return character \r :

// enter ' ____one_two\r\n ' to test New Scanner (system.in); Scanner.usedelimiter ("\ n" = scanner.next (); // Print Gets the contents and its length, and the ASCII code of the last character SYSTEM.OUT.PRINTLN (str + ":" + str.length ()        + "(" + Str.codepointat (Str.length ()-1) + ")");

Workaround: Use \r\n as a delimiter is normal

[To] Java: questions about the Usedelimiter () method of scanner

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.