5

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.

stealthjong
  • 10,858
  • 13
  • 45
  • 84
Haini
  • 922
  • 2
  • 12
  • 28
  • 4
    If there's no input available, what do you think `hasNext` should return? How is it meant to know whether there *will* be a next token or not, other than by blocking until it sees the end of the stream or the next token? – Jon Skeet Jun 01 '13 at 13:18
  • true if and only if this scanner has another token as the API says. Kind of strange... What should I use instead? Also it does work with every other hasNext Statement in my program - in every other case: No Input --> false --> break. – Haini Jun 01 '13 at 13:20
  • 1
    It's not clear what you're trying to do. You need to understand that a scanner is based on streaming input - if there's no data currently available but the stream isn't closed, then there might be more data later... and it's got to wait for that information in order to return anything useful. – Jon Skeet Jun 01 '13 at 13:21
  • You're using blocking IO, and you've made a request for input by calling `hasNext()`. It's going to block until there's input (and return true) or until the underlying stream is closed (and return false). – Brian Roach Jun 01 '13 at 13:24
  • It seems like you are Kind of right and I totally missunderstood the hasNext()... I did use it for checking for further entries. That's totally okay in a while Loop and makes sense if I want to check wether the next word is an Integer. Well, Kind of assumed the wrong Thing. Thanks for the tips, shall I delete this question due to not being really relevant to anyone else? – Haini Jun 01 '13 at 13:26
  • Check out here http://stackoverflow.com/a/44096029/3351097 . Looks like this is same problem as you have. – Maxian Nicu May 21 '17 at 10:58

3 Answers3

9

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.

dimo414
  • 47,227
  • 18
  • 148
  • 244
Haini
  • 922
  • 2
  • 12
  • 28
1

If you have nothing else to do while waiting for user input, then it's fine to be blocked at that call until the next input arrives.

If you do want to run other code while waiting for input, spawn a new thread and call hasNext and other blocking scanner methods from there.

rixo
  • 23,815
  • 4
  • 63
  • 68
  • 1
    I have to check a command for correct Syntax, for example: create int int String. if one of the three Parameters isn't entered an error should be shown. It seems like the Server that's testing my program enters data in a different way, most probably just reading in from an Textfile where no Change is expexcted and hasNext() Returns false. I tested via console --> hasNext() waits. Thanks anyways. – Haini Jun 01 '13 at 14:54
1

I'm not sure , but the following is my own experience :
when the Scanner object is fed with a file , it will not be blocking !
By the term "fed with a file " I mean that the scanner is constructed like this :
Scanner scanner = new Scanner("myFile.txt");

But if the scanner is constructed using the getInputStream()method of a Socket object , like this :

input = socket.getInputStream();
Scanner scanner = new Scanner(input);

the scanner will be blocking !

Shahryar Saljoughi
  • 2,599
  • 22
  • 41