0

I'm sure many have seen this program, but I'm wondering how to utilize a foreach loop when searching for an object in an array? If I enter anything, then search for the same string, it says it's not found. I don't mind using the usual for loop, I'm just trying the foreach loop out to get the hang of it. The program worked with the older for loop.

If any other code is needed for relevance, I will post it.

    private void searchFlowers(String flowerPack[]) {
    // TODO: Search for a user specified flower
    Scanner in = new Scanner(System.in);
    System.out.println("Please enter the name of the flower you are searching for: ");
    String flowerSearch = in.nextLine();

    //search the array
    for(String flower: flowerPack) { //
        if(flower.equals(flowerPack)) {
            System.out.println("You have that flower in your flower pack.");
            break;
        }
        else if(!flower.equals(flowerPack)){ //if it goes through the entire array without finding the string (flower)
            System.out.println("Did not find that in your flower pack.");
            break;
        }
    }

}
2
  • 1
    You are comparing apples with peaches ;). Replace flower.equals(flowerPack) with flower.equals(flowerSearch). Commented Aug 29, 2015 at 16:13
  • FlowerPack is an entire array, so you are comparing a single string with an array of strings (which will not return true). Also, your else if is unnecessary. If the if statement fails, you know they are not equal, so you don't need to check. This part isn't related to your problem, but you could replace the else if with just an else. Commented Aug 29, 2015 at 16:13

3 Answers 3

4

You have two problems:

  1. You're comparing flower with flowerPack; it should be flowerSearch.

  2. You're always breaking out of the loop, because you break out if flower.equals(flowerPack) and also if !flower.equals(flowerPack). But one of those will always be true.

So:

boolean found = false;
for(String flower: flowerPack) { //
    if(flower.equals(flowerSearch)) {
        found = true;
        break;
    }
}
if (found) {
    System.out.println("You have that flower in your flower pack.");
} else {
    System.out.println("Did not find that in your flower pack.");
}

There, we only exit the loop early if we find something. Otherwise we keep looping until we run out of things to check. The flag tells us whether we found it.

Side note: Your comparison is also case-sensitive, but people are notoriously bad at being consistent about capitalization. You might consider equalsIgnoreCase.

Sign up to request clarification or add additional context in comments.

2 Comments

I typically prefer equalsIgnoreCase, but for the sake of brevity, I just kept it like that for now.
@csheridan: It's too bad, really, it would have warned you about the flowerPack/flowerSearch thing. But... :-)
0

Change

if(flower.equals(flowerPack))

To

if(flower.equals(flowerSearch))

Update: Also see T.J. Crowder answer on why you have to remove your second check.

2 Comments

That's one problem.
Hah, true, there is much more wrong with the code. Just saw this part and was already set with whats wrong.
0
 Boolean flowerFound = false;
 for(String flower: flowerPack) { //
    if(flower.equals(flowerSearch)) {
        System.out.println("You have that flower in your flower pack.");
        flowerFound = true;
        break; 
    }
 }
 if(!flowerFound) {
   System.out.println("Did not find that in your flower pack.");
 }

Edits:

You used flower.equals(flowersPack). This is wrong. flowersPack is an array. So you were comparing a String (flower) with an array (flowersPack). What you wanted to do, however, was compare flower with flowerSearch.

I've removed the else-if clause from the for loop. It caused the loop to terminate if the first element in flowerPack is not equal to flower.

1 Comment

Oh wow.... That was extremely ignorant on my part. I know better than that. I must have been glancing at the foreach statement as I was typing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.