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
Post a Comment