Methods for Java Keyboard input

Source: Internet
Author: User

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

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: info-contact@alibabacloud.com 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.