How to Use System.in in Java
See Java: Tips and Tricks for similar articles.
At times you might need to read console input provided by the user from the keyboard. The System.in field permits you to read input from the keyboard. The input can be converted into a stream of characters and then buffered so that all characters up to but not including the Enter key can be presented to the program. To learn how to use System.in in Java, follow these four steps.
- Open your text editor and type in the following Java statements:
The program creates an InputStreamReaderobject withSystem.inas the constructor parameter.System.inprovides the input stream from the keyboard andInputStreamReaderreads the stream. Next, aBufferedReaderobject is instantiated with theInputStreamReaderobject passed to the constructor. TheBufferedReaderobject will buffer the input so that a line of text provided by the user is available to the program. The line of input is read using thereadLinemethod. This method can potentially throw the checked exceptionIOException, so the method call must be in a try catch block. - Save your file as UseSystemIn.java.
- Open a command prompt and navigate to the directory containing your Java program. Then type in the command to compile the source and hit Enter.

- Type in the command to run your program and hit Enter.
At the prompt you can type your name and then hit Enter. The readLineprovides the string data to the program. The program then displays your name, and this action verifies that the data was transferred to your program successfully.
