-2

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");
                    }
                }
            }
        }
Deniro
  • 33
  • 7
  • 2
    The very first thing: do a full stop now. Instead of trying to learn two complicated things at once (java and android!) you should focus on basic java first. And then: avoid writing such code - it is already overly complex ... and we could sit down and talk about ways to improve this (sorry) over-complicated code for an hour or so. But that is not what SO is meant for. Thus: start by asking yourself why you got **two** loops here (the outer seems to be enough) and why you introduced that inner loop ... but never use that *i* parameter?! – GhostCat Feb 25 '18 at 18:03

4 Answers4

0

You should add some label:

outerloop:
for(findpet_getItems items:arrayList) {
        for(int i=0; i <arrayList.size();i++) {
            ...
                    break outerloop;
            ...

        }
    }

And results.setText("Search not found"); should be moved out of these loops

anhtuannd
  • 918
  • 9
  • 16
  • 1
    No. The OP should step back and consider what he really wants to do. Maybe a labeled loop/break fixes the symptom. But the problem is that he most likely has no idea what he is doing, respectively how to do that in "sane" ways. So yes, your answer somehow addresses the direct issue - but it completely leaves out the part that the OP is well, not really knowing what he is doing. – GhostCat Feb 25 '18 at 18:04
  • Yes he should understand the problem first and I'm not sure the propose of sample code in this question. But if he needs way to stop, so here it is. – anhtuannd Feb 25 '18 at 18:08
  • That is the whole point: we have no idea what he wants. And when questions are off low quality, your answer should be crystal clear to make up for that ... – GhostCat Feb 25 '18 at 18:18
0

Try using early returns. Often the pattern will be

method() {
    if (condition) { block1; }
    else if (condition2) { block2; }
    else { block3; }
    return;
}

Try using an early return so the example looks more like:

method() {
  if(condition) { block1; return; }
  if(condition2) { block2; return; }
  block3; return; 
}
Craig Taylor
  • 1,689
  • 1
  • 11
  • 13
0

It's because your logic looks to be wrong:

This is stating if I is not 0 then do this, elf etc.

but, look at the logic here:

for(**int i=0**; i <arrayList.size();i++)
        {
            i**f(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;
                }

I will always be 0 on the first iteration of the loop, so wont execute anything, then it becomes 1 so the if becomes true, then follows the flow downwards:

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");
                }
            }

So, its backwards, change the logic to something more like:

or(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");
                }
            }else break;
        }
    }
Glennismade
  • 117
  • 2
  • 14
0

At case, If you want to break the loop you can use:

break;

Like:

if(true) break;

Or if you want to jump de conditions and continue the loop, you can use:

continue;

Like:

if(true) continue;

You'd see more in: Difference between break and continue statement or https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/

I hope to help you

Eric Saboia
  • 301
  • 2
  • 10
  • 22