8

I am trying to read lines of text from the console. The number of lines is not known in advance. The BufferedReader.readLine() method reads a line but after the last line it waits for input from the console. What should be done in order to avoid this?

Please see the code snippet below:

    public static String[] getLinesFromConsole() {
    String strLine = "";
    try {
        // Get the object of DataInputStream
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String line = "";
        while ((line = br.readLine()) != null)
            strLine += line + "~"; //edited

        isr.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return strLine.split("~");
}
Community
  • 1
  • 1
ambar
  • 2,053
  • 6
  • 27
  • 32
  • 1
    My guess is that the code never exits the while loop. And correctly so. An idea would be that when you read something specific, like "exit", you could break. – dkateros Jan 29 '13 at 10:53
  • 3
    Shouldn't that be `while ((line = br.readLine()) != null) strLine += **line** + "~";`? – OldCurmudgeon Jan 29 '13 at 10:54
  • you should use some condition to exit the while loop, for example having the user type exit – B11 Jan 29 '13 at 10:55
  • You appear to be skipping every second line, which I suspect is not correct and you are waiting for the end of the input stream which is difficult for most console users to type. ;) – Peter Lawrey Jan 29 '13 at 10:55
  • I try your code when you invoke readline second time while ((line = br.readLine()) != null) strLine += //here br.readLine() + "~"; console start to wait another character; you should use line variable OldCurmudgeon suggest this solution. And you can determine a exit command. – erhun Jan 29 '13 at 11:13
  • Corrected the skipping line part. It was a typo. strLine += **line** + "~"; – ambar Jan 29 '13 at 12:57
  • Is it possible to to end the readLine() execution without having an exit string? – ambar Jan 29 '13 at 13:00
  • This is duplicate question. See this: http://stackoverflow.com/questions/5506778/java-read-from-console-until-getting-a-blank-line – araut Sep 19 '14 at 22:53

2 Answers2

4

The below code might fix, replace text exit with your requirement specific string

  public static String[] getLinesFromConsole() {
    String strLine = "";
    try {
        // Get the object of DataInputStream
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String line = "";
        while ((line = br.readLine()) != null && !line.equals("exit") )
            strLine += br.readLine() + "~";

        isr.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return strLine.split("~");
}
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
1

When reading from the console, you need to define a "terminating" input since the console (unlike a file) doesn't ever "end" (it continues to run even after your program terminates).

There are several solutions to your problem:

  1. Put the input in a file and use IO redirection: java ... < input-file

    The shell will hook up your process with the input file and you will get an EOF.

  2. Type the EOF-character for your console. On Linux and Mac, it's Ctrl+D, on Windows, it's Ctrl+Z + Enter

  3. Stop when you read an empty line. That way, the user can simply type Enter.

PS: there is a bug in your code. If you call readLine() twice, it will skip every second line.

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820