hasNext() - when does it block and why?

前端 未结 3 1969
无人及你
无人及你 2020-11-29 12:43

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

3条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 13:03

    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.

提交回复
热议问题