Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ReadLine (Buffered reader) does not work in a loop

I’m writing code in Intelij. readLine does not correctly read the input (puts a line break at the end of the line), which is why the next readline does not work (becomes automatically empty).
Code:

public class Start {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String s = reader.readLine();
        ArrayList<String> list = new ArrayList<>();
        while (true) {
            if (s == null || s.isEmpty()) {
                break;
            }
            else list.add(s);
            s = reader.readLine();
        }

        for (String str : list) {
            System.out.println(str);
        }

If important: LF separator and UTF-8 encoding

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It’s a know bug. The fix is already available, please update.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading