1

I'm currently making a program where nested loops are needed to search through an array to find a spot for good input within the array. Here is an example:

public void placePiece(int column) {
        boolean goodInput = false;

        while(!goodInput) {
            for(int x = 5; x >= 0; x--) {
                if(boardStatus[x][column] == 0) {

                    setRow(x);
                    boardStatus[x][column] = 1;
                    goodInput = true;
                    break;

                }else if(boardStatus[0][column] == 1) { 
                    goodInput = false;
                    break;
                }else{

                }

            }
        }
    }

The method takes a parameter which is the column in which the piece should be located (received by a mouse listener). If the column in the 2D array is already filled to the top, the program gets stuck in an endless loop within the "else if", and I'm unsure how I would break out of this loop. How could I break out of this loop if there is bad input so that the user can try to give another column as input.

4
  • Using try/catch will help Commented Apr 24, 2015 at 21:23
  • 1
    @ryekayo since there is no error being caused, what will I be catching? Commented Apr 24, 2015 at 21:24
  • you will need to create your own Exception and catch what it is your looking for that is considered bad input Commented Apr 24, 2015 at 21:25
  • possible duplicate of Breaking out of nested loops in Java Commented Apr 24, 2015 at 21:32

5 Answers 5

8

An easy way is to use a labeled break statement.

myLabelName:
for(outer loop)
    for(inner loop)
        if(condition)
            break myLabelName;

This is useful when you'd rather not waste time iterating over other objects/items when you've found what you needed.


To expand, when you use just break; (without a label) it will exit the parent loop. Ex:

myLabelName:
for(outer loop){
    for(inner loop){
        if(condition){
            break myLabelName; //breaks outer loop
        }
        else if(other condition){
            break; //breaks parent (inner/nested) loop
        }
     }
 }
Sign up to request clarification or add additional context in comments.

Comments

0

When you are in elseif loop your are again assigning the goodInput = false; so the while conditions again becomes true and loops continuous forever.

if you want to take input till correct input is received can use do(){}while() loop

Comments

0

Based on the description you provided, I am not sure of the purpose of the else if statement. As I understand you intention, you are checking all of the rows within a certain column to see if that cell has been set or not?

The combination of the while loop and the for loop seems suspect as well. There doesn't seem to be any need for the while loop in this case.

What is supposed to happen if the entire column is filled? Should this method return a status flag?

Can't this be implemented with a single for loop?

for (int x = 5; x >= 0; --x)
{
    if(boardStatus[x][column] == 0) {
        setRow(x);
        boardStatus[x][column] = 1;
        break;
    }
}

If you want, you can track a flag variable to indicate if you ever found a cell which was empty.

Comments

0

you can use flag or you can use goto statement

flag description

code

bool  flag = true;
for (int i = 0; (i < 10) && (flag==true); i++)
{
   for(int j = 0; (j < 10) && (flag==true); j++)
   {
      if (condition)
         flag = false;
   }  
}

1 Comment

This looks like a C++ answer, can you format it so it matches Java syntax?
0

Change the parameter AFTER the loop:

public void placePiece(int column) {
    boolean goodInput = false;

    while(!goodInput) {
        for(int x = 5; x >= 0; x--) {
            if(boardStatus[x][column] == 0) {

                setRow(x);
                boardStatus[x][column] = 1;
                goodInput = true;
                break;

            }else if(boardStatus[0][column] == 1) { 
                goodInput = false;
                break;
            }else{

            }

        }
        goodInput = true; // <-----
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.