Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hasNext() - when does it block and why?

I'm trying to read commands via a Scanner Object. For checking the Input Syntax I use sc.hasNext() (for the case of missing commands). It did work fine for many cases already, but now I have the case that's described in the JavaAPI as "MAY block and wait for Input".

When does the hasNext() method block and how can I control it? The funny Thing is that it work's perfectly fine with 3 cases before the block. Also the JavaAPI describes hasNext() as the proper method for checking wether there is another Input or not so that the Method next() doesn't produce an Exception.

Here is the code I did produce till now:

if (sc.hasNext() && sc.next().equals("create")) {
    if (sc.hasNextInt()) {
        width = sc.nextInt();
        if (width > 0) {
            if (sc.hasNextInt()) {
                heigth = sc.nextInt();
                if (heigth > 0) {
                    if (sc.hasNext()) {  //At this point the hasNext Statement blocks for //no reason till an Input is made.
                        charset = sc.next();
                        Image = new AsciiImage(width, heigth,charset);
                    } else {
                        ret = false;
                        System.out.println("INPUT MISMATCH");
                    }
                } ...//and so on

Thanks in advance, I couldn't find anything on this Topic an my own. Edit: The Scanner is defined as a System.in, but that shouldn't be a Problem - at least it hasn't been one till now.

like image 575
Haini Avatar asked Jun 01 '13 13:06

Haini


People also ask

What does hasNext () do in Java?

The hasNext() method checks if the Scanner has another token in its input. A Scanner breaks its input into tokens using a delimiter pattern, which matches whitespace by default. That is, hasNext() checks the input and returns true if it has another non-whitespace character.

What will SC hasNext () return when the scanner reaches the end of the file?

The hasNext() is a method of Java Scanner class which returns true if this scanner has another token in its input.

How do I stop hasNext?

The only way you can get hasNext() to return false is by terminating the input. In the case of file input, that means you have passed the last line.

What does iterator hasNext do?

The hasNext() method of ListIterator interface is used to return true if the given list iterator contains more number of element during traversing the given list in the forward direction.


1 Answers

There is a difference between testing via Console or via TextFile. If I read from Console the program expects a Stream and it will wait for further Input.

When testing via Input from Textfile (still with System.in for the Scanner, using Java Program ) the hasNext() will return false at the end of the file as no further Input can be done.

I can't really find documentation (in https://docs.oracle.com/javase/9/docs/api/java/util/Scanner.html#hasNext--) on this Topic. So if anyone finds a proper and technical correct answer I would be very greatfull.

like image 151
Haini Avatar answered Nov 16 '22 06:11

Haini