1. Three ways to achieve keyboard input
System.in the standard input stream. is to get the data from the keyboard
keyboard input data three ways :
a: The args receive parameter of the Main method.
Java HelloWorld Hello World Java
B : Scanner (JDK5 later)
Scanner sc = new Scanner (system.in);
String s = sc.nextline ();
int x = Sc.nextint ()
C : standard input stream implementation via character buffer stream packaging
BufferedReader br = new BufferedReader (new InputStreamReader (system.in));
2. Code implementation:
1 Packagecn.itcast_04;2 3 ImportJava.io.BufferedReader;4 Importjava.io.IOException;5 ImportJava.io.InputStreamReader;6 7 /*8 * system.in standard input stream. is to get the data from the keyboard9 * Ten * Keyboard input data: One * A:The args receive parameter of the Main method. A * java HelloWorld Hello world Java - * B:Scanner (after JDK5) - * Scanner sc = new Scanner (system.in); the * String s = sc.nextline (); - * int x = Sc.nextint () - * C: implementation of standard input stream via character Buffer stream packaging - * BufferedReader br = new BufferedReader (new InputStreamReader (system.in)); + */ - Public classSystemindemo { + Public Static voidMain (string[] args)throwsIOException { A // //get standard input stream at //InputStream is = system.in; - // //Do I have to get one row at a time? - // //line. - // //How to achieve it? - // //To achieve this, first you need to know which way to read one row of data at a time? - // //ReadLine () in // //and which class is this method in? - // //BufferedReader to // //So, you should create the BufferedReader object this time, but the underlying is still using the standard input stream + // //bufferedreader br = new BufferedReader (is); - // //according to our supposition, it should be possible now, but it's an error. the // //The reason for this is that the character buffer stream can only be manipulated for character streams, and you are now a byte stream, so it can't be used? * // //So, I still want to use, please give me a solution? $ // //converts a byte stream to a character stream, and then flows through a character bufferPanax Notoginseng //InputStreamReader ISR = new InputStreamReader (is); - //BufferedReader br= New BufferedReader (ISR); theBufferedReader br =NewBufferedReader (NewInputStreamReader (system.in)); + ASystem.out.println ("Please enter a string:"); theString line =br.readline (); +System.out.println ("The string you entered is:" +Line ); - $System.out.println ("Please enter an integer:"); $ //int i = Integer.parseint (Br.readline ()); -line =br.readline (); - inti =Integer.parseint (line); theSystem.out.println ("The Integer you entered is:" +i); - }Wuyi}
Run the effect as follows:
Java Fundamentals Hardening IO Stream Note 62: Three ways to implement keyboard entry