• Post Reply Bookmark Topic Watch Topic
  • New Topic

if( I can't figure this out....)  RSS feed

 
Carol Murphy
village idiot
Bartender
Posts: 1211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. I'm working on a servlets assignments that requires the use of Cookies. Cookies are stored in an Array, and the only way to pick out the cookie you're searching for is to iterate through the array until you find it. Now, I know I should be able to figure this out on my own, but my brain has gone numb. Here's the deal:

I want to stop the for loop once the cookie with the name "id" is found, if it is found, and have isValid return true. If the cookie is not found, isValue should remain false. How can I iterate through the array and stop when I've found the right cookie? Maybe a while loop instead of a for loop???
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carol,
I would go with the following:



Two other alternatives spring to mind:
1) Use a "break" statement in the if statement after setting isValid. I don't particularly like that approach because it makes the code harder to follow and more brittle.
2) Use the while loop you suggested. That's a harder approach, because you are now in charge of incrementing the int counter and writing the while loop condition to check the counter and isValid status.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also make the condition part of the for loop

[ September 01, 2006: Message edited by: Garrett Rowe ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!