It is convenient to use Scanner to get user input, but it separates each input string with blank space, which does not apply at some point because the user may enter a string with blank characters in the middle, and you want to get the complete string.
You can use the BufferedReader category, which is a class provided in the java.io package, so when using this class, you must first import the java.io package; the readLine() method of the BufferedReader object must handle IOException exceptions (exception). The exception handling mechanism is provided by Java to program designers to catch possible errors in the program. At this stage, your method to deal with IOException is to add throws IOException after main() method. This will be discussed in detail in the future.
BufferedReader accepts a Reader object when building. When reading the standard input stream, it uses the InputStreamReader, which inherits the Reader class. You use the following method to create a buffer object for the standard input stream:
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
The "new" keyword means that you want to build an object for you. The BufferedReader buf means that you declare an object variable of type BufferedReader, while the new BufferedReader() means that you construct an object with the BufferedReader class. newInputStreamReader(System.in) means that you accept a System.in object to construct an InputStreamReader object.
You can look at this paragraph after learning the object orientation concept. If you are difficult to understand at this stage, remember the above buffer reading object creation method. Usually, BufferedReader is used to obtain user input. This is what I wrote.
The following program can obtain user input (can include blank byte input) in text mode and redisplay it in the main console:
import java.io.*;public class GetInput {public static void main(String[]args) throws IOException {BufferedReader buf = newBufferedReader( newInputStreamReader(System.in));System.out.print("Please enter a column of text: ");String text = buf.readLine();System.out.println("The text you entered: " + text);}}The readLine() method will pass back all byte inputs before the user presses the Enter key, excluding the last pressed Enter return bytes. The execution example of the program is as follows:
Please enter a column of text: This is a test!
The text you entered: This is a test!
The above is all about obtaining input input string instances in Java. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!