permaculture playing cards*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 19541
    
  16

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about scanner.hasNext(".")
 
Similar Threads
Scanner
How can I replace the line of file?
Simple File IO Question...Please Help!!!
Problem with PrintWriter and another Problem with Inputhandling
Parsing text file into 3 columns