0

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Laura
  • 9
  • 1
  • 1
    I cannot reproduce. I got `s1gg s2false s3s1`. – Ole V.V. May 21 '22 at 08:21
  • Is that your full code? Someting similar may happen sometimes if you interleave different calls to a `Scanner`. Please see [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Ole V.V. May 21 '22 at 08:23
  • really?did you press the enter key after each word? – Laura May 21 '22 at 08:28
  • @Laura I've updated my answer. This has more to do with the fact that the enter corresponds to two white space characters (\n, \r) rather than one. That link brings to a Scanner question while she is using a BufferedReader. The Scanner object is not even used. – Dan May 21 '22 at 08:31
  • @Laura I made sure to press the return key on my Mac after each line. – Ole V.V. May 21 '22 at 08:40
  • On which operating system are you running your code and typing your input? – Ole V.V. May 21 '22 at 09:04
  • @Laura What happens if you run your program from the command prompt (Terminal on Mac, “DOS prompt” on Windows) rather than in IntelliJ IDEA or other IDE? – Ole V.V. May 21 '22 at 11:40

2 Answers2

2

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.

Dan
  • 3,647
  • 5
  • 20
  • 26
1

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

Yuvaraj R
  • 107
  • 1
  • 5
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Sumit Sharma May 23 '22 at 06:59