1 ImportJava.io.BufferedReader; 2 Importjava.io.IOException; 3 ImportJava.io.InputStreamReader; 4 ImportJava.util.Scanner; 5 6 Public classEntertest {7 8 Public Static voidMain (string[] args) {//Main Method9Chartest ();//calling the System.in methodTenReadtest ();//calling the Readtest method OneScannertest ();//calling the Scannertest method A } - /** - * System.in and System.out methods the * Disadvantage One: The method can get the characters entered from the keyboard, but only for one character of the acquisition - * Disadvantage two: Only the type of char is obtained. 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 (); ASystem.out.println ("Yout Enter Char is:" +i); at } - Catch(IOException e) { - e.printstacktrace (); - } - - } in /** - * InputStreamReader and BufferedReader methods to * Advantages: Can get the keyboard input string + * Cons: How to get the type of int,float is still needed to convert - */ the Public Static voidreadtest () { *System.out.println ("Readtest, please Enter Data:"); $InputStreamReader is =NewInputStreamReader (system.in);//new construct InputStreamReader objectPanax NotoginsengBufferedReader br =NewBufferedReader (IS);//take the tectonic approach to the BufferedReader. - Try{//there is a Ioexcepiton in this method that needs to be captured theString name =br.readline (); +System.out.println ("Readtest Output:" +name); A } the Catch(IOException e) { + e.printstacktrace (); - } $ $ } - /** - * Methods in the Scanner class the * Advantage One: can get the keyboard input string - * Advantage Two: There is ready to obtain int,float and other types of data, very powerful, also very convenient;Wuyi */ the Public Static voidscannertest () { -Scanner sc =NewScanner (system.in); WuSystem.out.println ("Scannertest, please Enter Name:"); -String name = Sc.nextline ();//reading string input AboutSystem.out.println ("Scannertest, please Enter Age:"); $ intAge = Sc.nextint ();//Read integer input -System.out.println ("Scannertest, please Enter Salary:"); - floatSalary = Sc.nextfloat ();//read float type input -SYSTEM.OUT.PRINTLN ("Your information is as below:"); ASystem.out.println ("Name:" + name + "\ n" + "Age:" +age + "\ n" + "Salary:" +salary); + } the}
Java keyboard input in several ways, Big Love scanner