kokokok
kokokok

Reputation: 479

Java while loop not looping

I have the following button in my swing interface

    private void solveButtonMouseClicked(java.awt.event.MouseEvent evt) {                                         


    step1(); 


}   

which calls step1();

   private void step1(){

//step 1 solving the white cross around the yellow middle
        while( (!(F2.getBackground().equals(white))) & (!(F4.getBackground().equals(white))) & (!(F6.getBackground().equals(white))) & (!(F8.getBackground().equals(white)))){


         textArea.append("cnt");

        //top three pieces solved
        if(F2.getBackground().equals(white) && F4.getBackground().equals(white) && F6.getBackground().equals(white)){
            //solve F8 
            while(!(F8.getBackground().equals(white))){

                //check right side
                if(R2.getBackground().equals(white)){right(); right(); downinv(); rightinv(); rightinv();}
                else if(R4.getBackground().equals(white)){rightinv(); downinv(); right();}
                else if(R6.getBackground().equals(white)){right(); downinv(); rightinv();}
                else if(R8.getBackground().equals(white)){downinv();}
                //check left side
                else if(L2.getBackground().equals(white)){left(); left(); down(); leftinv(); leftinv();}
                else if(L4.getBackground().equals(white)){leftinv(); down(); left();}
                else if(L6.getBackground().equals(white)){left(); down(); leftinv();}
                else if(L8.getBackground().equals(white)){down();}
                //check back side
                else if(B2.getBackground().equals(white)){back(); back(); down(); down();}
                else if(B4.getBackground().equals(white)){backinv(); down(); down();}
                else if(B6.getBackground().equals(white)){back(); down(); down();}
                else if(B8.getBackground().equals(white)){down(); down();}
                //check up side
                else if(U2.getBackground().equals(white)){frontinv(); up(); rightinv(); upinv();}
                else if(U4.getBackground().equals(white)){frontinv(); up(); up(); rightinv(); upinv(); upinv();}
                else if(U6.getBackground().equals(white)){frontinv(); rightinv();}
                else if(U8.getBackground().equals(white)){front();}
                //check down side
                else if(D2.getBackground().equals(white)){down(); frontinv(); right();}
                else if(D4.getBackground().equals(white)){front(); right();}
                else if(D6.getBackground().equals(white)){frontinv(); leftinv();}
                else if(D8.getBackground().equals(white)){downinv(); frontinv(); right();}
            }//end while
        }//end if

        //top two pieces solved
        if(F2.getBackground().equals(white) && F4.getBackground().equals(white)){
            //solve F6
            while(!(F6.getBackground().equals(white))){
                //chech front side
                if(F8.getBackground().equals(white)){front();}
                //check right side
                else if(R2.getBackground().equals(white)){frontinv(); up(); front();}
                else if(R4.getBackground().equals(white)){right(); frontinv(); up(); front();}
                else if(R6.getBackground().equals(white)){rightinv(); frontinv(); up(); front();}
                else if(R8.getBackground().equals(white)){right(); right(); frontinv(); up(); front();}
                //check left side
                else if(L2.getBackground().equals(white)){frontinv(); upinv(); front();}
                else if(L4.getBackground().equals(white)){frontinv(); left(); upinv(); leftinv(); front();}
                else if(L6.getBackground().equals(white)){frontinv(); leftinv(); upinv(); left(); front();}
                else if(L8.getBackground().equals(white)){frontinv(); left(); left(); upinv(); left(); left(); front();}
                //check back side
                else if(B2.getBackground().equals(white)){frontinv(); up(); up(); front();}
                else if(B4.getBackground().equals(white)){frontinv(); backinv(); up(); up(); front();}
                else if(B6.getBackground().equals(white)){frontinv(); back(); up(); up(); front();}
                else if(B8.getBackground().equals(white)){frontinv(); back(); back(); up(); up(); front();}
                //check up side
                else if(U2.getBackground().equals(white)){up();rightinv();upinv();}
                else if(U4.getBackground().equals(white)){up(); up();rightinv(); upinv(); upinv();}
                else if(U6.getBackground().equals(white)){rightinv();}
                else if(U8.getBackground().equals(white)){upinv(); rightinv(); up();}
                //check down side
                else if(D2.getBackground().equals(white)){down(); right();}
                else if(D4.getBackground().equals(white)){down(); down(); right();}
                else if(D6.getBackground().equals(white)){right();}
                else if(D8.getBackground().equals(white)){downinv(); right();}
            }//end while
            textArea.append("\nstep 1a (75% complete)\n");
        }//end if

        //top piece solved
        if( (F2.getBackground().equals(white)) && (!(F4.getBackground().equals(white))) ){
            //solve F4
            while(!(F4.getBackground().equals(white))){
                //check front side
                if(F6.getBackground().equals(white)){frontinv();}
                //check right side
                else if(R2.getBackground().equals(white)){front(); up(); frontinv();}
                else if(R4.getBackground().equals(white)){right(); front(); up(); frontinv();}
                else if(R6.getBackground().equals(white)){rightinv(); front(); up(); frontinv();}
                else if(R8.getBackground().equals(white)){right(); right(); front(); up(); frontinv();}
                //check left side
                else if(L2.getBackground().equals(white)){front(); upinv(); frontinv();}
                else if(L4.getBackground().equals(white)){front(); left(); upinv(); frontinv();}
                else if(L6.getBackground().equals(white)){front(); leftinv(); upinv(); frontinv();}
                else if(L8.getBackground().equals(white)){front(); left(); left(); upinv(); frontinv();}
                //check back side
                else if(B2.getBackground().equals(white)){front(); up(); up(); frontinv();}
                else if(B4.getBackground().equals(white)){front(); backinv(); up(); up(); frontinv();}
                else if(B6.getBackground().equals(white)){front(); back(); up(); up(); frontinv();}
                else if(B8.getBackground().equals(white)){front(); back(); back(); up(); up(); frontinv();}
                //check up side
                else if(U2.getBackground().equals(white)){front(); front(); up(); rightinv(); frontinv(); frontinv();}
                else if(U4.getBackground().equals(white)){left();}
                else if(U6.getBackground().equals(white)){front(); front(); rightinv(); frontinv(); frontinv();}
                else if(U8.getBackground().equals(white)){front(); front(); upinv(); rightinv(); frontinv(); frontinv();}
                //check down side
                else if(D2.getBackground().equals(white)){downinv(); leftinv();}
                else if(D4.getBackground().equals(white)){leftinv();}
                else if(D6.getBackground().equals(white)){downinv(); downinv(); leftinv();}
                else if(D8.getBackground().equals(white)){down(); leftinv();}
            }//end while
            textArea.append("\nstep 1a (50% complete)\n");
        }//end if

        if(!(F2.getBackground().equals(white))){ 
            //solve top piece

          while(!(F2.getBackground().equals(white))){
                if(F4.getBackground().equals(white)){front();}
                else if(F6.getBackground().equals(white)){frontinv();}
                else if(F8.getBackground().equals(white)){front(); front();}
                //check right side
                else if(R2.getBackground().equals(white)){up();}
                else if(R4.getBackground().equals(white)){right(); up();}
                else if(R6.getBackground().equals(white)){rightinv(); up();}
                else if(R8.getBackground().equals(white)){right(); right(); up();}
                //check left side
                else if(L2.getBackground().equals(white)){upinv();}
                else if(L4.getBackground().equals(white)){left(); up();}
                else if(L6.getBackground().equals(white)){leftinv(); up();}
                else if(L8.getBackground().equals(white)){left(); left(); upinv();}
                //check back side
                else if(B2.getBackground().equals(white)){up(); up();}
                else if(B4.getBackground().equals(white)){backinv(); up(); up();}
                else if(B6.getBackground().equals(white)){back(); up(); up();}
                else if(B8.getBackground().equals(white)){back(); back(); up(); up();}
                //check up side
                else if(U2.getBackground().equals(white)){up(); rightinv(); frontinv();}
                else if(U4.getBackground().equals(white)){up(); up(); rightinv(); frontinv();}
                else if(U6.getBackground().equals(white)){rightinv();frontinv();}
                else if(U8.getBackground().equals(white)){upinv(); rightinv(); frontinv();}
                //check down side
                else if(D2.getBackground().equals(white)){down(); right(); frontinv();}
                else if(D4.getBackground().equals(white)){down(); down(); right(); frontinv();}
                else if(D6.getBackground().equals(white)){right(); frontinv();}
                else if(D8.getBackground().equals(white)){downinv(); right(); frontinv();}


           }//end while
           textArea.append("\nStep 1a (25% complete)\n");
        }//end else
    }//end while
}//end step 1

The problem seems to be the outermost while loop in step1(). It is supposed to keep running until all the condition are met but it just stops after one iteration and I can't figure out why. If I remove the outer while loop step1() proceeds exactly as it should going through each step on multiple presses of the button. Help!

Upvotes: 0

Views: 2511

Answers (4)

GregA100k
GregA100k

Reputation: 1395

You mention that you want to keep looping until all the comparisons are true.

The way it's coded now will drop out of the loop if any condition becomes true.

Rather than

while ( !boolean1 && !boolean2 && !boolean3) {

Try

while ( ! (boolean1 && boolean2 && boolean3) ) {

Upvotes: 5

karim79
karim79

Reputation: 342625

Maybe one of your up/down/left/right/left/leftinv etc. methods is modifying the background(s) you're testing the colours of, causing the while condition to evaluate to false thereby preventing more than 1 iteration.

Upvotes: 1

Imagist
Imagist

Reputation: 18514

My guess is that the JVM reaches that wall of text and, much like us, has no idea what to do with it, nor any desire to.

That, or you're using & instead of &&.

But honestly, if you're looping through and applying a gigantic branching else-if ladder, you've done something pretty severely wrong. Even if you get it to work, this kind of code is hell on the processor, not to mention being a pain to maintain. My recommendation: refactor.

Upvotes: 0

Jeff Barger
Jeff Barger

Reputation: 1239

My first guess is that it looks like you are using the bitwise '&', rather than the logical comparison '&&' in your loop.

Upvotes: 0

Related Questions