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