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