dimanche 25 février 2018

How to stop other "else if conditions" from executing if 1st "if condition" is true

Its been bugging me for a day now and I don't know how to do this because I'm still new to java and android. I have this code that has a if and else if statement in a for loop. Now the problem is that if the 1st condition is true it will also execute the other condition below the 1st which is not what I want. I want to stop the loop when the above condition is true and exit it.

So can anyone please tell me what is wrong.

for(findpet_getItems items:arrayList)
        {
            for(int i=0; i <arrayList.size();i++)
            {
                if(i !=0){
                    if (items.getHair().toLowerCase().contains(hair) && items.getColor().toLowerCase().contains(color)
                            && items.getSize().toLowerCase().contains(size) && items.getWeight().toLowerCase().contains(weight)
                            ) {
                        new_list.add(items);
                        break;
                    }
                    else if (items.getSize().toLowerCase().contains(size) && items.getWeight().toLowerCase().contains(weight)) {
                        new_list.add(items);
                        break;
                    }

                    else if (items.getHair().toLowerCase().contains(hair) && items.getColor().toLowerCase().contains(color)) {
                        new_list.add(items);
                        break;
                    }
                    else {
                        results.setText("Search not found");
                    }
                }
            }
        }

Aucun commentaire:

Enregistrer un commentaire