Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about scanner.hasNext(".")

 
Barry Burd
Author
Ranch Hand
Posts: 118
5
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
Pie
Posts: 20510
54
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