aspose file tools*
The moose likes Beginning Java and the fly likes confused about for loops with arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "confused about for loops with arrays" Watch "confused about for loops with arrays" New topic
Author

confused about for loops with arrays

Bill Suttle
Ranch Hand

Joined: Oct 22, 2012
Posts: 37
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

Joined: Oct 22, 2012
Posts: 37
I just needed to insert a break. Resolved. I could not find a place to delete this thread.

Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confused about for loops with arrays