Beginner Java, perhaps we have encountered a problem, from the console to get the characters, the most common is through System.in.read (); Get the characters you entered, the code is as follows:
public static void Receiveonechar () {//Gets an input character
char ch= ' 2 ';
System.out.println ("Please enter a number:");
try {
ch= (char) System.in.read ();
} catch (IOException e) {
e.printstacktrace ();
}
SYSTEM.OUT.PRINTLN ("Your entered char is:" +ch);
}
But this method can only get an input character, in order to get more input characters, or to achieve a,b addition how do we achieve it? Look below:
We know that Java is input into the stream, where the system class manages the basic input output stream and the error stream.
System.out the stream to the default location
System.in Get input
System.err Output error message
Each of these three objects is automatically generated whenever the main method is executed;
The basic input stream InputStream is an abstract class that has the following methods for inheriting subclasses:
Read (); Reading data from the stream;
Skip (); Skip several bytes in the stream;
Available (); Returns the number of bytes available in the stream;
Mark (); marks a position in the stream;
Reset (); Returns the position of the marker;
Marksupport (); Whether to support marking and reset operation;
Close ();
Among them, the read () method has three versions;
int read (); read an integer;
int read (byte[] b); Read multiple bytes to an array of bytes;
int read (byte[] b,int off,int len); Read len-length bytes from one location of the byte array;