Hi, so this is my program. All is working fine expcet stoping reading from console. Problem is that even after you write done if you are fast enought you are able to write more. As you can see I can't close scanner because of two inputs. Even so I tried to close it but problem was still here.
Any ideas how to fix it? Thank you in advance for your answers.
public static void main(String...args) { System.out.println("Write first input:"); System.out.print(streamToString(System.in, "")); System.out.println("Write secondinput:"); System.out.print(streamToString(System.in, "")); } public static String streamToString(InputStream input, String errorMessage) { Scanner scanner = new Scanner(input); StringBuilder data = new StringBuilder(""); while (scanner.hasNext()) { String line = scanner.nextLine(); if (line.equals("done")) { System.out.println("Reading from console finished."); break; } else { data.append(line + "\n"); } } if (data.length() > 0) { data.deleteCharAt(data.length() - 1); } if (data.toString().equals("")) { if (!errorMessage.equals("")) { System.out.println(errorMessage); } return ""; } else { return data.toString(); } }