Java iterator problem

34 views
Skip to first unread message

hofar...@houseoffusion.com

unread,
Dec 4, 2013, 6:19:54 PM12/4/13
to ColdFusion Technical Talk

Hi,

I am attempting to use Google's libphonenumber library to find, match and
parse phone numbers within text strings.

I load the libphonenumber jar using Javaloader and create an instance of
the PhoneUtil class.

I create a test string of:

"Here is one phone number (212) 555-1212 and here is a second 312-555-1212
and a third +1 404 555 1212"

I then call PhoneUtil.findNumbers(testString,"US"). This should return
matches of phone numbers in pretty much any recognized US format within the
string.

The return from the findNumbers() method has an iterator() method. So it
seems to me I should be able to loop and dump the matched objects, with
something like:

<cfloop condition="result.iterator().hasNext()">
<cfdump var="#result.iterator().next()#">
</cfloop>

However, this creates what appears to be an infinite loop. I'm guessing
it's because the loop isn't really looping through the iterator's
collection, but rather simply reading a static "true" from the hasNext()
initial value, and always dumping the first entry in the iterator's
collection.

Of course, I know so little about Java, I could be completely wrong.

But if one of you folks who understands better than I how to manipulate
Java within ColdFusion could point out the error of my ways, I'd certainly
appreciate it.



--
Thanks,

Tom

Tom McNeer
MediumCool
http://www.mediumcool.com
1735 Johnson Road NE
Atlanta, GA 30306
404.589.0560


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:357282

hofar...@houseoffusion.com

unread,
Dec 4, 2013, 6:23:24 PM12/4/13
to ColdFusion Technical Talk

> The return from the findNumbers() method has an iterator() method. So it
> seems to me I should be able to loop and dump the matched objects, with
> something like:
>
> <cfloop condition="result.iterator().hasNext()">
> <cfdump var="#result.iterator().next()#">
> </cfloop>
>
> However, this creates what appears to be an infinite loop. I'm guessing
> it's because the loop isn't really looping through the iterator's
> collection, but rather simply reading a static "true" from the hasNext()
> initial value, and always dumping the first entry in the iterator's
> collection.

What happens if you just rewrite the loop so that you check hasNext()
within the body of the loop and exit the loop if there's no next?

Dave Watts, CTO, Fig Leaf Software
http://www.figleaf.com/
http://training.figleaf.com/

Fig Leaf Software is a Veteran-Owned Small Business (VOSB) on
GSA Schedule, and provides the highest caliber vendor-authorized
instruction at our training centers, online, or onsite.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:357283

hofar...@houseoffusion.com

unread,
Dec 4, 2013, 6:36:07 PM12/4/13
to ColdFusion Technical Talk

Your method is the way I�ve always looped Java iterators in the past, with one exception: I�ve always created the variable for the iterator before using it in the loop. What happens with:

<cfset resultIterator = result.iterator()>
<cfloop condition="resultIterator.hasNext()">
<cfdump var="#result.iterator().next()#">
</cfloop>

I�m not good with the �under-the-hood� java of what is happening here, but it seems to me that by placing the your result variable in the loop, you are re-starting the loop with each sequence.

HTH,
Jon
Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:357284

hofar...@houseoffusion.com

unread,
Dec 4, 2013, 6:53:57 PM12/4/13
to ColdFusion Technical Talk

And the winning answer is: Jon's method!!

I'm sure you're right about restarting the loop.

And thanks, Dave.



--
Thanks,

Tom

Tom McNeer
MediumCool
http://www.mediumcool.com
1735 Johnson Road NE
Atlanta, GA 30306
404.589.0560


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:357285
Reply all
Reply to author
Forward
0 new messages