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 (); Calling the System.in method
Readtest (); Calling the Readtest method
Scannertest ();//Call scannertest method
}
/**
* System.in and System.out methods
* 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 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 methods
* Advantages: Can get the keyboard input string
* Cons: How to get the type of int,float is still needed to convert
*/
public static void Readtest () {
System.out.println ("Readtest, please Enter Data:");
InputStreamReader is = new InputStreamReader (system.in); New construct InputStreamReader Object
BufferedReader br = new BufferedReader (IS); Take the tectonic approach to the BufferedReader.
try{//There is a Ioexcepiton in this method that needs to be captured
String name = Br.readline ();
System.out.println ("Readtest Output:" + name);
}
catch (IOException e) {
E.printstacktrace ();
}
}
/**
* Methods in the Scanner class
* 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;
*/
public static void Scannertest () {
Scanner sc = new Scanner (system.in);
System.out.println ("Scannertest, please Enter Name:");
String name = Sc.nextline (); Reading string input
System.out.println ("Scannertest, please Enter Age:");
int age = Sc.nextint (); Read integer input
System.out.println ("Scannertest, please Enter Salary:");
float salary = 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);
}
}
Three ways to input from the keyboard in Java