Three ways to receive keyboard input in Java _java

Source: Internet
Author: User
Tags advantage
Import Java.io.BufferedReader;  
Import java.io.IOException;  
Import Java.io.InputStreamReader;  
 
Import Java.util.Scanner;  public class Entertest {public static void main (string[] args) {//Main method Chartest ();  Call the System.in method Readtest (); Call the Readtest method Scannertest ()//Call Scannertest method}/** * System.in and System.out method * Disadvantage One: This method can get the characters entered from the keyboard, but only Gain * Disadvantage two for one character: gets only the char type. 
   If you want to get int,float and other types of input, more trouble. 
      */public static void Chartest () {try{System.out.print ("Enter a Char:"); 
      char i = (char) System.in.read (); 
    System.out.println ("Yout Enter Char is:" + i); 
    catch (IOException e) {e.printstacktrace (); 
   }/** * InputStreamReader and BufferedReader method * Advantages: Can get the keyboard input string * Disadvantage: How to get the int,float and other types of still need to convert 
    */public static void Readtest () {System.out.println ("readtest, please Enter Data:"); InputStreamReader is = new InputStreamReader (system.in); New Construction InputstreamreAder object BufferedReader br = new BufferedReader (IS); 
      Get the constructed method into the BufferedReader try{//There is a Ioexcepiton in the method that needs to capture String name = Br.readline (); 
    System.out.println ("Readtest Output:" + name); 
    catch (IOException e) {e.printstacktrace (); The method in/** * Scanner class * Advantage One: can get keyboard input string * Advantage Two: Have ready-made access to the type of data such as Int,float, very powerful, but also very convenient; * * Publi 
    c static void Scannertest () {Scanner sc = new Scanner (system.in); 
    System.out.println ("Scannertest, please Enter Name:");  String name = Sc.nextline (); 
    Reads the string input System.out.println ("Scannertest, please enter Age:");    int age = Sc.nextint (); 
    Read int input System.out.println ("Scannertest, please enter Salary:"); float salary = sc.nextfloat (); 
    Reads the float type input System.out.println ("Your information is as below:"); 
  System.out.println ("Name:" + name + "\ n" + "Age:" +age + "\ n" + "Salary:" +salary);  } 
}

Summary:
to get input from the keyboard:
Python provides, Python2 has Raw_input (), Python3 has input ().
C provides the scanf () function
C + + provides the CIN () function to get keyboard input
There is no ready-made function in Java to get keyboard input, but it can still be implemented using the above method, where method three should be the simplest and most convenient.

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.