0

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);
9
  • 1
    "I mean (scanner.hasNext()) is always true." it is not always true. It just waits for data since System.in is still opened, meaning that new data is still possible. But if you close System.in (in windows you could do it with Ctrl+Z combination - assuming you are running it from console) it will see that new data can't come and will return false causing loop to exit. Commented Nov 11, 2020 at 20:48
  • Ctrl + Z doesn't work in InetlliJ...... Commented Nov 11, 2020 at 20:50
  • But to avoid relying on user knowledge about how to close System.in (or to simply not close it since we may want to read from it later) we usually add explicit exit loop value like stop exit etc. to break from loop. Commented Nov 11, 2020 at 20:50
  • So you could rewrite that loop into something more like boolean stopAsking = false; while(!stopAsking && scanner.hasNext()){String nextGuest = scanner.next(); if(nextGuest.equals("EXIT")){ stopAsking = true; } else { questList.add(nextGuest);} }. Commented Nov 11, 2020 at 20:53
  • 1
    Yes, but don't forget to (A) negate result of hasNext like while(!scanner.hasNext("EXIT")){..} and (B) use next() method after loop to read that value from Scanner (assiming you may want to use it somewhere else in your application). Remember that any hasABC methods will not consume any value, it is next, nextLine or nextTYPE which let scanner move to next token. Commented Nov 11, 2020 at 21:01

2 Answers 2

1

You can break the infinite loop based on some condition e.g. in the code given below, I have used while(true){} which is an infinite loop which will run until the user enters quit (case-insensitive).

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<String> questList = new ArrayList<>();

        while (true) {// Infinite loop
            String nextGuest = scanner.next();
            if (nextGuest.equalsIgnoreCase("quit")) {
                break;
            }
            questList.add(nextGuest);
        }

        Collections.reverse(questList);

        questList.forEach(System.out::println);
    }
}

A sample run:

a1
b2
c3
quit
c3
b2
a1
Sign up to request clarification or add additional context in comments.

Comments

0

The while loop will keep going while the scanner has another token in its input. Inside the while loop, you are advancing the scanner with scanner.next(), so the scanner isn't stationary. So eventually scanner.hasNext() will be False.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.