This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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:
Thank you in advance, arrays are new for me so I'm still pretty confused about working with them.
Joined: Oct 22, 2012
I just needed to insert a break. Resolved. I could not find a place to delete this thread.