-1

I am developing a program that takes input from the console. So far it has been no problem reading input that consists of one line of input from the console. But the program does not work when it is supposed to read multiple lines. How can i improve the readInput method to read multiple lines of input and the return a single String containing all of the input from different lines.

private String readIntput() throws IOException {
    BufferedReader inputstream = new BufferedReader(new InputStreamReader(System.in));
    String input = inputstream.readLine();

    return input;
}
Kyssmark
  • 21
  • 4
  • 1
    you can try putting it in a loop `while ((input=inputStream.readLine())!=null) { sb.append (input);}` – ACV Sep 24 '20 at 10:05
  • 1
    How do you know how many lines you *want* to real? Is it always same amount of lines, or do you recognize it some other way? – Pshemo Sep 24 '20 at 10:07
  • 1
    *"How to read multiple lines of input and the return a single String"* --- You call `readLine()` **multiple times** and **concatenate** the lines into a single string, with newlines added between the lines. --- Which part is troubling you? Doing something multiple times? Concatenating strings? Adding newlines? – Andreas Sep 24 '20 at 10:16
  • 1
    @ACV You forgot to add the newlines that are stripped by `readLine()` – Andreas Sep 24 '20 at 10:17
  • Does this answer your question? [How can I read a large text file line by line using Java?](https://stackoverflow.com/questions/5868369/how-can-i-read-a-large-text-file-line-by-line-using-java) – Polygnome Sep 24 '20 at 10:20
  • You should be saving the Reader so that it can be used to read multiple lines. As is, you're creating a new BufferedReader for each line you read. – NomadMaker Sep 24 '20 at 10:36
  • Your BufferedReader is just left hanging at the end of your function. This means that it can be garbage collected which will probably close it, which will also close the resources it uses. This will close System.in, which will prevent you from reading from there until you quit and reopen your program, – NomadMaker Sep 24 '20 at 11:00

1 Answers1

1

So when you write String input = inputstream.readLine() This reads one line at a time, As you are taking input from the user there would not be any null cases even if the user clicks enter, You need to check for the length of the input string, If it is 0 then break from the while loop.

But this isn't the case when you are reading from a file or other source you need to check whether the input is null or not.

Hope this could help you.

private String readIntput() throws IOException {
    BufferedReader inputstream = new BufferedReader(new InputStreamReader(System.in));
    StringBuilder finalString = new StringBuilder();
    String input = inputstream.readLine();
    while(true){
       finalString.append(input);
       input=inputstream.readLine();
       if(input.length() == 0){
           break;
       }
    }
    br.close();
    return finalString;
}

Input:

hi hello
how are you
Am fine

Output:

hi hellohow are youAm fine
Swapnil Padaya
  • 687
  • 5
  • 14
  • From the console it is possible for the user to type control-d (at the beginning of a line) to close the input. – NomadMaker Sep 24 '20 at 11:01
  • Could you give one example, that would be more helpful for me, Thanks – Swapnil Padaya Sep 24 '20 at 11:05
  • Why don't you try it? While entering input from the console, to close the stream, you just type control-d (just like control-c, but holding down the control key and the d key simultaneously). – NomadMaker Sep 24 '20 at 11:07
  • I tried doing so, but there was no effect of either control+c or control + d. Though I know control + c terminates the program. Am using eclipse to do so but thanks for adding this to my knowledge. Am figuring out why control + d isn't working in eclipse console. – Swapnil Padaya Sep 24 '20 at 11:52