• 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

confused about for loops with arrays

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

This is a simple little program where the user enters a name and that name is sent to a class with a pre-defined array of strings. If the name user entered is in the array, then the method getName() returns the name to the user, otherwise getName() tells user name is not there.

My problem is iterating through the names in the array and then returning the correct answer to the user. I'm using a for loop and the way I have it set up, it goes through all iterations -- even if say it finds the correct answer on the first or second iteration. The end result then is that whether the loop returns the correct answer is relative to where the answer is in the iteration and not whether the answer is genuinely correct.

For example, in the program below, if user searches for 'Ricky', the program returns correct answer [it was the last in the iteration]. But if user types in 'Trey' or 'Melissa', it returns 'Name not found' because it finds those names on the first and second iteration and then on the third iteration (there are three elements in the array) the value of my result resets to 'not found'.

How can I get the for loop to return the correct result, even if the result is not on the last iteration?

Here is my code:



and...



Thank you in advance, arrays are new for me so I'm still pretty confused about working with them.


 
Bill Suttle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just needed to insert a break. Resolved. I could not find a place to delete this thread.

Thanks.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bill Suttle wrote: . . . Resolved.

Well done

I could not find a place to delete this thread.

Thanks.

There isn’t one. We don’t believe in deleting threads.
 
reply
    Bookmark Topic Watch Topic
  • New Topic