Read standard input in Java

This article is based on the motivation of other powers, "I don't know the best practices, so if you write them in Qiita for the time being, a detailed person will tell you" rather than "showing the best practices yourself". Masu (´ ・ ω ・ `) A test that tries to follow the collective intelligence of the Internet.

First of all, the one that uses BufferedReader is often found in blog articles on the Internet.

var stdin = new BufferedReader(new InputStreamReader(System.in));

//Read standard input line by line.
String line;
while ((line = stdin.readLine()) != null) {
    doSomething(line);
}

//If the value given from the standard input is a numerical value, you need to convert it yourself.
while ((line = stdin.readLine()) != null) {
    int i = Integer.valueOf(line);
    doSomething(i);
}

//Easy to connect to stream.
stdin.lines().forEach(System.out::println);
stdin.lines().mapToInt(Integer::valueOf);

To briefly summarize the advantages and disadvantages of this method,

--Easy to find samples on the net. This "rich sample" is not surprising in practical programming. ――However, compared to the Scanner described later, there are more types, and script-level programming is a little troublesome. ――It's hard to say that it's beautiful even if you understand it when you write the constructor twice (´ ・ ω ・ `) --Easy to connect to Stream introduced from Java8. --Since it is buffered, it seems to work at high speed (unconfirmed) --You need to handle the checked exception IOException. This is a bit annoying at a little script level.

By the way, personally speaking, 90% of competitive programming uses standard input in Java. In short, there are many cases where you want to write a little and move it a little. In addition, it is common in competitive programming to use an input format such as "the first line is a numerical value, and the second and subsequent lines are character strings". In such cases, it is necessary to quickly handle various types of input. I think that Scanner is often more convenient in such cases.

var stdin = new Scanner(System.in);

//Read standard input line by line.
while (stdin.hasNextLine()) {
    var line = stdin.nextLine();
    doSomething(line);
}

//For example, if you have the following input---> 1000 10.005 HOGE
//Even such a complicated input is relatively easy to process.
int i = stdin.nextInt();
double d = stdin.nextDouble();
String s = stdin.nextLine();

//It's a little difficult to connect to Stream. In order to use mapToObj etc., some ingenuity is required.
stdin.forEachRemaining(System.out::println);

Here too, I will summarize the advantages and disadvantages.

--Anyway, it's multifunctional. Easy to handle complex inputs. ――I can't help but think, "I don't understand because it's too multifunctional." Aside from competitive programming, I wonder if there is such a complicated input in practice. ――Since it does not throw inspection exceptions, it is convenient when you want to write a little and move it a little. --It's hard to connect to Stream. It's hard to use Function anyway. --Whether it is buffered depends on the implementation. Probably not (unconfirmed)


Writing this way, both seem to have advantages and disadvantages (naturally). The latter is easier for a bit of script-level programming, but the former is definitely more modern. It seems good to adopt the former in practical programming (´ ・ ω ・ `)

Recommended Posts

Read standard input in Java
Read JSON in Java
Read binary files in Java 1
Read binary files in Java 2
Very simple input reception in Java
Read Java properties file in C #
Read CSV in Java (Super CSV Annotation)
Story of paiza.jp [Java standard input solution]
Partization in Java
Changes in Java 11
[Java] Use cryptography in the standard library
Rock-paper-scissors in Java
Receive joystick input in Java LW JFL
Java standard logger
Read Felica using RC-S380 (PaSoRi) in Java
Read xlsx file in Java with Selenium
Console input in Java (understanding the mechanism)
Pi in Java
FizzBuzz in Java
Ruby standard input
[Java] How to receive numerical data separated by spaces in standard input
Read a string in a PDF file with Java
[Java] How to get and output standard input
Add, read, and delete Excel comments in Java
[Java] Read the file in src / main / resources
[java] sort in list
Interpreter implementation in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Read Java Property file
Callable Interface in Java
java standard functional interface
Comments in Java source
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Read Java HashMap source
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
java Scanner loop input