• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

next() and hasNext() methods of Scanner class

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

The JLS says that "Both hasNext and next methods may block waiting for further input. Whether a hasNext method blocks has no connection to whether or not its associated next method will block." What does "being blocked waiting for input to be scanned mean"? Can someone perhaps quote an example code snippet ?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try that with different inputs, including lots of whitespace and blank lines followed by the enter key.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Try that with different inputs, including lots of whitespace and blank lines followed by the enter key.


How do I provide blank lines as user input without making the cursor disappear. The cursor goes away when I press return key and it displays the first token as a result of executing the above snippet.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the enter key before you write anything.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Use the enter key before you write anything.


OK. So unless and until I enter the input , the hasNext/next method will not be invoked. Got it. Thanks..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic