Java keyboard input Scanner class

Source: Internet
Author: User

There is less direct interaction with the keyboard in normal work, but in some test methods it is necessary to have such a function.

One, you can use the System.in.read () method to read a single character, but because of the size of the character limit, there are many inconvenient in the actual use of the place.

Example:

char a = (char) System.in.read (); System.out.println (a);

Be aware that the ASCII code that corresponds to the character is returned. Further processing is required to be used.

Second, the console input can be treated as a string, you need to use the BufferedReader class and the InputStreamReader class.

Example:

New BufferedReader (new= strbufferedreader.readline (); System.out.println (strstring);

Note that when you use ReadLine (), you can receive spaces and tabs, so you can't use spaces and tabs as a two-digit split.

Third, the use of scanner class

The scanner class is very powerful, providing read basic types, while the Nextline () method provides a string fetch.

New Scanner (system.in); int a = aascanner.nextint (); System.out.println ("111_" += aascanner.nextline (); System.out.println ("AAA" += aascanner.nextline (); System.out.println ("BBB" +strstring1);

It should be noted that the nextline () here can receive the carriage return, in the process of writing the program, you need to pay attention to the case of the carriage return received.

Java keyboard input Scanner class

Related Article

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.