Wrong scanner example in Java book?

I practice HashSet from Java book by Cay S. Horstmann and Gary Cornell and I think there is a bug in the sample code on page 687. We have Scanner

importing words in HashSet

and it looks like this (I removed unnecessary code to make the problem is more noticeable):

Set<String> words = new HashSet<String>();
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
    String word = in.next();
    words.add(word);
}

      

The problem is that there is no way to stop this cycle. Or maybe I'm missing something there?

To stop the loop, I added another static helper method:

public static boolean isStop(Scanner in) {
    if (in.next().equals("stop")) {
        return true;
    }
    return false;
}

      

And now the main code looks like this:

Set<String> words = new HashSet<String>();
Scanner in = new Scanner(System.in);
while (!isStop(in)) {
    String word = in.next();
    words.add(word);
}

      

Is there any other way to stop the scan cycle? Can't believe the author of the book made a mistake?

+3


source to share


5 answers


The loop stops as soon as this condition is false:

in.hasNext()

      

Ie, no more words.



Inside the loop, there is a command to read the following word:

in.next()

      

This way, the words will continue until the scanner reads more words. This cycle will end at the end of what the scanner is reading.

+3


source


As you scan System.in

, the loop doesn't stop as it will keep adding words to yours Set

, but there is no visible error in the program.

Your idea of ​​choosing a keyword to stop the loop when user input matches that keyword sounds good.

You don't need a static method that takes an argument to do this Scanner

.



Just add the following after String word = in.next();

:

if (word.equalsIgnoreCase("stop")) {
    System.out.printf("Quitting with set: %s%n", words);
    in.close();
    return; // assuming method is void
}

      

+1


source


The scanner will continue to run as long as there are still words in the input, as others explain. Note that when we talk about System.in

, it usually waits until the user has typed more text, and therefore will not stop until the user closes the stream (supplies the appropriate end of file for the operating system). On Unix / Linux, the user will need to use control-D to complete the loop.

+1


source


The loop does not stop as it will keep adding words to yours Set

, but there is no visible error in the program.

Your idea of ​​choosing a keyword to stop the loop when user input matches that keyword sounds good.

You don't really need a static method using your argument Scanner

to do this.

Just add a change to your code like this:

Set<String> words = new HashSet<String>();
Scanner in = new Scanner(System.in);
System.out.print("Type a word...");
while (in.hasNext()) {
    String word = in.next();
    if (word.equalsIgnoreCase("stop")) {
        System.out.printf("Quitting with set: %s%n", words);
        return; // assuming method is void
    }
    else {
        words.add(word);
        System.out.print("Type a word (or \"stop\" to quit)...");
    }
}

      

+1


source


IF there is code to be executed after the loop, then yes, I would say the authors made a mistake. Write to them about it! If this is an example of adding elements to Set

, then the example is fine. It all depends on what was the goal of the authors in this example.

hasNext()

is a blocking method meaning it will always wait for more input. There are related questions about this. Your way of "fixing it" is what the general consensus did.

+1


source







All Articles