Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: java.util.Scanner weirdness
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: java.util.Scanner weirdness



Ansgar Esztermann wrote:

Still, should not findInLine() in principle work in the way I expected it to? Unless I made some blatant mistake there, I'll file a bug.


Read the source for Scanner.

findInLine() eventually calls findWithinHorizon().

findWithinHorizon() is documented as using non-anchoring bounds.

"The scanner treats the horizon as a transparent, non-anchoring bound (see {@link Matcher#useTransparentBounds} and {@link Matcher#useAnchoringBounds})."

The description of Matcher.useAnchoringBounds() makes it clear that ^ and $ meta-chars are only recognized when using anchoring bounds. Therefore, findWithinHorizon() appears to not recognize ^ and $, and it does so intentionally (i.e. the behavior is documented).

I have no idea whether this matching behavior is correct, incorrect, or debatably-correct. I'm just saying this is what the source and docs show is happening, and the fact it's documented suggests it's intentional. Maybe consult a Scanner tutorial.

Personally, I wouldn't use a Pattern to match a single literal like "ATOM". I'd just use readLine() on the Reader and string.startsWith (). Or readLine() and then apply an anchored Matcher to the line if the possible starting patterns were more than a single literal. Scanner appears to intentionally set the pattern to non-anchoring (search its source for useAnchoring), which doesn't seem very line- oriented to me.

  -- GG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.