Reprint: http://blog.csdn.net/u012249177/article/details/49586383
Java Input methods:
ImportJava.io.BufferedReader; Importjava.io.IOException; ImportJava.io.InputStreamReader; ImportJava.util.Scanner; Public classEntertest { Public Static voidMain (string[] args) {//Main MethodChartest ();//calling the System.in methodReadtest ();//calling the Readtest methodScannertest ();//calling the Scannertest method } /*** system.in and System.out method * Disadvantage One: The method can get the characters entered from the keyboard, but only for one character to get * Disadvantage two: get only char type. If you want to get int,float and other types of input, it is more troublesome. */ Public Static voidchartest () {Try{System.out.print ("Enter a Char:"); Chari = (Char) System.in.read (); System.out.println ("Yout Enter Char is:" +i); } Catch(IOException e) {e.printstacktrace (); } } /*** InputStreamReader and BufferedReader method * Pros: Can get keyboard input string * Disadvantage: How to get the type of int,float and still need to convert*/ Public Static voidreadtest () {System.out.println ("Readtest, please Enter Data:"); InputStreamReader is=NewInputStreamReader (system.in);//new construct InputStreamReader objectBufferedReader br =NewBufferedReader (IS);//take the tectonic approach to the BufferedReader. Try{//there is a Ioexcepiton in this method that needs to be capturedString name =Br.readline (); System.out.println ("Readtest Output:" +name); } Catch(IOException e) {e.printstacktrace (); } } /*** method in scanner class * Advantage One: can get keyboard input string * Advantage Two: There is ready to obtain int,float and other types of data, very powerful, but also very convenient;*/ Public Static voidscannertest () {Scanner SC=NewScanner (system.in); System.out.println ("Scannertest, please Enter Name:"); String name= Sc.nextline ();//reading string inputSystem.out.println ("Scannertest, please Enter Age:"); intAge = Sc.nextint ();//Read integer inputSystem.out.println ("Scannertest, please Enter Salary:"); floatSalary = Sc.nextfloat ();//read float type inputSYSTEM.OUT.PRINTLN ("Your information is as below:"); System.out.println ("Name:" + name + "\ n" + "Age:" +age + "\ n" + "Salary:" +salary); } }
Methods for Java Keyboard input