Laura
Laura

Reputation: 9

Second readLine() returns empty string on Intellij IDEA

This is the code I used:

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in) );
String s1 = reader.readLine();
String s2 = reader.readLine();
String s3 = reader.readLine();
System.out.format("s1%s s2%b s3%s",s1,s2.equals(""),s3);

When I input the following text in the console, the second result is an empty string.

gg
ss
s1gg s2true s3ss

Anyone knows why this happens?

I tested the program in the IDEA on Mac.

Upvotes: 0

Views: 275

Answers (2)

dani-vta
dani-vta

Reputation: 6890

After further investigations, it seems like that the odd behavior you're describing happens only when multiple read lines are stacked one after the other and executed via Intellij IDEA. Apparently, even regardless of the operating system being used, since you're experiencing this issue on a Mac whereas in my case it occurs on a Windows machine.

I've tested your exact same code on other IDEs too, like Netbeans and Visual Studio Code, ultimately also manually compiled it with javac and executed it via cmd, and none of these cases have shown the same behavior happening on Intellij IDEA.

EDIT

Apparently, as it has been pointed out in the comments, this is indeed an issue of Intellij IDEA version 2022.1.1. The link below also says that it will be fixed with the version 2022.1.2.

https://youtrack.jetbrains.com/issue/IDEA-293951/Console-readLine-skips-input-in-2022-1-1

So, to answer your question this is not a problem with the code you've written but rather an issue with the IDE you're using.

Upvotes: 2

Yuvaraj R
Yuvaraj R

Reputation: 117

It is indeed a bug in Intellij IDEA console as mentioned in Dan's answer. This was already reported and fixed as per their tracker. Fix should be available in 2022.1.2 version.

https://youtrack.jetbrains.com/issue/IDEA-293951/Console-readLine-skips-input-in-2022-1-1

Until then you can try to use any command line terminal to test the program which is working perfectly fine as expected.

enter image description here - Test result with Build #IC-221.5591.52

Upvotes: 1

Related Questions