This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Question about scanner.hasNext( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question about scanner.hasNext(".")" Watch "Question about scanner.hasNext(".")" New topic
Author

Question about scanner.hasNext(".")

Barry Burd
Author
Ranch Hand

Joined: Jun 18, 2003
Posts: 118
    
    5

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?



Java For Dummies, 6th Edition, Java Programming for Android Developers For Dummies
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question about scanner.hasNext(".")
 
Similar Threads
How can I replace the line of file?
Problem with PrintWriter and another Problem with Inputhandling
Scanner
Parsing text file into 3 columns
Simple File IO Question...Please Help!!!