Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in a while loop

 
Mah pish
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be helpful if you posted the pattern and text you are using to test this.

Hunter
 
Mah pish
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20776
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew, It works fine now
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic