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;
}
}
}
flower.equals(flowerPack)withflower.equals(flowerSearch).