Java: Get Standard Input from System.in without Scanner


I have summarized a method to directly obtain input using System.in.read() without using java.util.Scanner in Java.

Environment

  • Java 11

[Review] How to Get Standard Input Using Scanner

To obtain standard input using Scanner, you do the following. Create an instance by passing System.in to the constructor of Scanner.

The Scanner instance is convenient, not only with next but also with methods that return values as numbers, such as nextInt, nextDouble, and methods that return values as booleans, such as nextBoolean.

How to Get Standard Input Directly from System.in

To obtain standard input directly from System.in, use System.in.read().

StandardInput.read() allows you to obtain the string of standard input. It does not return a value until Enter is pressed in standard input. System.in returns each character one by one until Enter is pressed ('\n' is entered).