Java keyboard input in several ways, Big Love scanner

Source: Internet
Author: User

1; 2; 3; 4 ImportJava.util.Scanner; 5 6 Public classEntertest {7 8 Public Static voidMain (string[] args) {//Main Method9Chartest ();//calling the methodTenReadtest ();//calling the Readtest method OneScannertest ();//calling the Scannertest method A } - /** - * 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) (); 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 (;//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 (; 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.