java - Testing a buffered reader reading from input stream -


i have problems when testing code. think problem has using bufferedreader reading inputstreamreader. used intellij , give following input:

hello world! 

why program not printing anything? here code:

public static void main(string[] args) throws ioexception {     bufferedreader br = new bufferedreader(new inputstreamreader(system.in));     deque<string> lines = new arraydeque<>();     string line = br.readline();      while (line != null) {         lines.push(line);         line = br.readline();     }      while (!lines.isempty()) {         system.out.println(lines.pop());     } } 

your code stunk in first loop.

to fix this, modify loop condition next:

while (line != null && !line.isempty()) {     lines.push(line);     line = br.readline(); } 

then loop exit when hit enter.

or can add other exit code. such while (line != null && !line.equals("exit")). when enter in console exit code(exit in example above) loop stop , desired output.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -