Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about scanner.hasNext(".")  RSS feed

 
Barry Burd
Author
Ranch Hand
Posts: 133
10
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I figure it, the following code should output



because the "data" file contains five characters, none of which have yet been read. Instead, the code outputs



Why?

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the input file look like? IIRC Scanner breaks up the input based on whitespace by default, so a "." would match only a single character.

(This is from memory--don't trust me.)
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right David:
Returns true if the next token matches the pattern constructed from the specified string.

Since a token is constructed using whitespace as delimiter hasNext(".") would indeed return true only if the token was a single character.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!