File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Problem in a while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem in a while loop" Watch "Problem in a while loop" New topic
Author

Problem in a while loop

Mah pish
Greenhorn

Joined: May 12, 2011
Posts: 7
hi Guy,
I have a code that reads a text file and searches for certain words with certain patterns and returns them. It works fine. The only problem is that it doesnt return the first word, Although all the words have the same pattern. this is the while code:


Any help would be appreciated.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

It would be helpful if you posted the pattern and text you are using to test this.

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Mah pish
Greenhorn

Joined: May 12, 2011
Posts: 7
Thanks Hunter, This is the code:



and the string testing would be something like this:


the code return:
test-3333-4444

it skips the first one which is :
test-1111-2222





Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Mah pish wrote:and the string testing would be something like this:


That isn't what I would expect as the result of


Not unless the method is really badly named, that is. I suggest you look at that a little more carefully. I know nothing about this StringUtils class but it's possible that the result of that substringBetween call is

and not

as you seem to be assuming.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

The first one matches fine - on line 38. But you then throw the match away. It's only the second and subsequent matches that are being output in the while loop.
Mah pish
Greenhorn

Joined: May 12, 2011
Posts: 7
Paul Clapham wrote:Sorry , i'd forgotten some parts of the html code
Mah pish wrote:and the string testing would be something like this:


That isn't what I would expect as the result of


Not unless the method is really badly named, that is. I suggest you look at that a little more carefully. I know nothing about this StringUtils class but it's possible that the result of that substringBetween call is

and not

as you seem to be assuming.
Mah pish
Greenhorn

Joined: May 12, 2011
Posts: 7
So what should be changed in the loop?
Matthew Brown wrote:The first one matches fine - on line 38. But you then throw the match away. It's only the second and subsequent matches that are being output in the while loop.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

Well, you could do something like this:

The key thing is to only call matcher.find() in the loop condition, not anywhere else.
Mah pish
Greenhorn

Joined: May 12, 2011
Posts: 7
Thanks Matthew, It works fine now
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

You're welcome!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in a while loop