• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also make the condition part of the for loop

[ September 01, 2006: Message edited by: Garrett Rowe ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic