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.
CodePudding user response:
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.
I think this might be an issue worth to be reported. In my case, I've tested the code on Intellij IDEA build 11.0.14.1 1-b2043.45.
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.
CodePudding user response:
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