0

Ive got a while loop that keeps running as long as an if is true at least once per loop but i cant find the reason why the loop wont stop. (there are a couple more ifs but i removed them because they are the exact same)

boolean changed = true;
    do{
        boolean stopLoop = false;

        if(remove1=="")
        {
            EquipmentSlot1.setText(EquipmentSlot2.getText());
            EquipmentSlot2.setText("");
            stopLoop = true;
        }
        if(remove2=="")
        {
            EquipmentSlot2.setText(EquipmentSlot3.getText());
            EquipmentSlot3.setText("");
            stopLoop = true;
        }
        if(remove3=="")
        {
            EquipmentSlot3.setText(EquipmentSlot4.getText());
            EquipmentSlot4.setText("");
            stopLoop = true;
        }
        if(stopLoop = false)
        {
            changed = false;
        }
    }while(changed);    

2 Answers2

2

Change

if (stopLoop = false)

to

if (stopLoop == false)

Note that = is for assignment, == for comparison.

Or better yet (and recommended):

if (!stop)

Edit:

Also, to compare Strings, use equals() method:

if (remove1.equals(""))
...
Community
  • 1
  • 1
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0

First, your last if is an assignment, not a test of equality, i.e. stopLoop = false should be == false

Second, changed has no way to be false in any if you have shown.

Cheung Brian
  • 715
  • 4
  • 11
  • 29