Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confused about for loops with arrays

 
Bill Suttle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I just needed to insert a break. Resolved. I could not find a place to delete this thread.

Thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic