1 Public classIO {2 Public Static voidMain (string[] args)throwsThrowable {3 scannertest ();4 //testScanner1 ();5 //TestScanner2 ();6 7 //Testinread ();8 9 //Testtext (); Ten } One A /** - * Methods in the Scanner class - * Advantage One: can get the keyboard input string the * Advantage Two: There is ready to obtain int,float and other types of data, very powerful, also very convenient; - * */ - Public Static voidscannertest () { -Scanner sc =NewScanner (system.in); +System.out.println ("Scannertest, please Enter Name:"); -String name = Sc.nextline ();//reading string input +System.out.println ("Scannertest, please Enter Age:"); A intAge = Sc.nextint ();//Read integer input atSystem.out.println ("Scannertest, please Enter Salary:"); - floatSalary = Sc.nextfloat ();//read float type input -SYSTEM.OUT.PRINTLN ("Your information is as below:"); -System.out.println ("Name:" + name + "\ n" + "Age:" +age + "\ n" + "Salary:" +salary); - } - in - /** to * Note: see below two examples to verify the time we read the data + * There are two ways to read data: Separating data with a carriage return and separating data with spaces - * Enter delimited data: The time to read the data is at each execution of Scanner.nextint () the * Space-delimited data: The time to read data is when Scanner.nextint () is first executed * * However, the above rules are not valid for reading into string types, because spaces are also strings $ * In addition, this type does not support reading charactersPanax Notoginseng */ - Private Static voidTestScanner1 () { theScanner Scanner =NewScanner (system.in); +System.out.println ("--1--"); A intL=scanner.nextint (); the int[] num =New int[l]; +System.out.println ("--2--"); - for(inti = 0; I<l && Scanner.hasnext (); i++){ $System.out.println ("--3--"); $Num[i] =scanner.nextint (); - } -SYSTEM.OUT.PRINTLN ("Array output:" +arrays.tostring (num)); the } - Wuyi Private Static voidTestScanner2 () { theScanner s =NewScanner (system.in); -System.out.println ("--1--"); Wu while(S.hasnext ()) { -System.out.println ("--2--"); About intA =s.nextint (); $System.out.println ("--3--"); - intb =s.nextint (); -System.out.println ("--4--"); -System.out.println ("Plus and:" + (A +b)); A } + } the - /** $ * System.in Method: the * This method can only be read into one character at a time, even if the receive type is int, it cannot read more than two-bit int type the * So this method is most suitable to read the character type, if you want to read int/float and other types, it will be very troublesome. A lot of limitations . the * */ the Public Static voidTestinread ()throwsioexception{ - CharB = (Char) System.in.read (); inSystem.out.println ("Number of char Type B:" +b); the } the About /** the * InputStreamReader and BufferedReader methods the * Input Output stream to read keyboard input the * This method is powerful for reading strings + * But if you want to program char/int/float, you also need to force type conversions - * */ the Public Static voidTesttext ()throwsthrowable{BayiInputStreamReader in =NewInputStreamReader (system.in); theBufferedReader br =NewBufferedReader (in); theString str =br.readline (); -System.out.println ("str:" +str); - } the}
Java Keyboard io