Found the solution below on Hyperskills.org (considered as correct solution) - can anybody explain how this code can work if while loop is endless? I mean (scanner.hasNext()) is always true.
Scanner scanner = new Scanner(System.in);
ArrayList<String> questList = new ArrayList<>();
while (scanner.hasNext()) {
String nextGuest = scanner.next();
questList.add(nextGuest);
}
Collections.reverse(questList);
questList.forEach(System.out::println);
System.inis still opened, meaning that new data is still possible. But if you closeSystem.in(in windows you could do it withCtrl+Zcombination - assuming you are running it from console) it will see that new data can't come and will returnfalsecausing loop to exit.stopexitetc. to break from loop.boolean stopAsking = false; while(!stopAsking && scanner.hasNext()){String nextGuest = scanner.next(); if(nextGuest.equals("EXIT")){ stopAsking = true; } else { questList.add(nextGuest);} }.hasNextlikewhile(!scanner.hasNext("EXIT")){..}and (B) usenext()method after loop to read that value from Scanner (assiming you may want to use it somewhere else in your application). Remember that anyhasABCmethods will not consume any value, it isnext,nextLineornextTYPEwhich let scanner move to next token.