posted 11 years ago
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.