Guy2
Guy2

Reputation: 21

ArrayIndexOutOfBoundsException: -1

So I am writing a code with a 2D array to arrange it like a square table say 10 by 10. It is filled with Xs and Os and blanks. A threshold is entered and if a percentage of indexes around the X or O are also Xs or Os are greater than the threshold than the spot is satisfied, if not it is unsatisfied. I have been able to print the table fine, but then I tried to do the satisfying part and I got an array index out of bounds exception. I have an idea of what this means but not sure how to get my code working properly, although I fear I would have to redesign it. Another thing is, I am not sure if I did the booleans right.

public class CellSim{

public static void main(String[] args){
    char [][] tissue = new char [10][10];
    int threshold = 30;
    assignCellTypes(tissue, 50, 25);
    printTissue(tissue);
    System.out.println();

    boolean boardSat = true;
    boardSat = boardSatisfied(tissue, threshold);

    if( boardSat == false){
        System.out.println( "board is not satisfied");}
    if( boardSat == true){
        System.out.println("board is satisfied");}



}


public static void printTissue(char[][] tissue){
    for(int row = 0;row < tissue.length;row++){
        for(int col = 0;col < tissue[row].length;col++){
            System.out.print(tissue[row][col] + "\t");
        }
        System.out.println();
    }
}


public static void assignCellTypes(char[][] tissue, int percentBlank, int percentX){
int n = (tissue.length) * (tissue.length);
percentBlank = (int) Math.ceil(n * (percentBlank * .01));
percentX = (int) Math.ceil((n - percentBlank) * (percentX * .01));
int percentO = (int) Math.ceil(n - percentBlank - percentX);

for( int i = 0; i < percentBlank; i++){
while(percentBlank > 0){
    int randCell = randInt(0, 9);
    int randCell2 = randInt(0, 9);
                if(tissue[randCell][randCell2] == '\u0000'){
                    tissue[randCell][randCell2] = ' ';
                    break;
                    }
}
}
for( int i = 0; i < percentX; i++){
while(percentX > 0){
    int randCell = randInt(0, 9);
    int randCell2 = randInt(0, 9);
                if(tissue[randCell][randCell2] == '\u0000'){
                    tissue[randCell][randCell2] = 'X';
                    break;
                    }
}
}
for( int i = 0; i < percentO; i++){
while(percentO > 0){
    int randCell = randInt(0, 9);
    int randCell2 = randInt(0, 9);
                if(tissue[randCell][randCell2] == '\u0000'){
                    tissue[randCell][randCell2] = 'O';
                    break;
                    }
}
}

}
public static boolean isSatisfied(char[][] tissue, int row, int col, int threshold){
    int total = 0;
    int same = 0;
    if(tissue[row][col] == 'X'){
        total = 0;
            if(tissue[row + 1][col - 1] == 'X'){
                same ++;
                total ++;
            }else if(tissue[row + 1][col - 1] == 'O')
                total ++;
            if(tissue[row + 1][col] == 'X'){
                same ++;
                total ++;
            }else if(tissue[row + 1][col] == 'O')
                total ++;
            if(tissue[row + 1][col + 1] == 'X'){
                same ++;
                total ++;
            }else if(tissue[row + 1][col + 1] == 'O')
                total ++;
            if(tissue[row][col - 1] == 'X'){
                same ++;
                total ++;
            }else if(tissue[row][col - 1] == 'O')
                total ++;
            if(tissue[row][col + 1] == 'X'){
                same ++;
                total ++;
            }else if(tissue[row][col + 1] == 'O')
                total ++;
            if(tissue[row - 1][col - 1] == 'X'){
                same ++;
                total ++;
            }else if(tissue[row - 1][col - 1] == 'O')
                total ++;
            if(tissue[row - 1][col] == 'X'){
                same ++;
                total ++;
            }else if(tissue[row - 1][col] == 'O')
                total ++;
            if(tissue[row - 1][col + 1] == 'X'){
                same ++;
                total ++;
            }else if(tissue[row - 1][col + 1] == 'O')
                total ++;

    }
    if(tissue[row][col] == 'O'){
        total = 0;
            if(tissue[row + 1][col - 1] == 'O'){
                same ++;
                total ++;
            }else if(tissue[row + 1][col - 1] == 'X')
                total ++;
            if(tissue[row + 1][col] == 'O'){
                same ++;
                total ++;
            }else if(tissue[row + 1][col] == 'X')
                total ++;
            if(tissue[row + 1][col + 1] == 'O'){
                same ++;
                total ++;
            }else if(tissue[row + 1][col + 1] == 'X')
                total ++;
            if(tissue[row][col - 1] == 'O'){
                same ++;
                total ++;
            }else if(tissue[row][col - 1] == 'X')
                total ++;
            if(tissue[row][col + 1] == 'O'){
                same ++;
                total ++;
            }else if(tissue[row][col + 1] == 'X')
                total ++;
            if(tissue[row - 1][col - 1] == 'O'){
                same ++;
                total ++;
            }else if(tissue[row - 1][col - 1] == 'X')
                total ++;
            if(tissue[row - 1][col] == 'O'){
                same ++;
                total ++;
            }else if(tissue[row - 1][col] == 'X')
                total ++;
            if(tissue[row - 1][col + 1] == 'O'){
                same ++;
                total ++;
            }else if(tissue[row - 1][col + 1] == 'X')
                total ++;


    }
    if(tissue[row][col] == ' '){
    return true;
    }if(total == 0){
        return false;
        }else if(((same / total) * 100) >= threshold){
        return true;
    }else{ return false;}
}       

 public static boolean boardSatisfied(char[][] tissue, int threshold){
    boolean isSat = true;
    while( isSat == true){
        for(int row = 0;row < tissue.length;row++){
            for(int col = 0;col < tissue[row].length;col++){
            isSat = isSatisfied(tissue, row, col, threshold);

            }
        }
    }
        if(isSat == false){
        return false;
        }else{return true;} 
}


public static int randInt(int min, int max){

   int range = (max - min) + 1;     
   return(int)(Math.random() * range) + min;
}



}

Upvotes: 0

Views: 1133

Answers (2)

destudent
destudent

Reputation: 21

You should check row, col to be >=0 and < tissue.size. Same for row/col +/-1. And also you can refactor your code in isSatisfied method by moving out total++ and reducing conditions together. Consider using regexps here.

Upvotes: 1

David.Jones
David.Jones

Reputation: 1411

The problem lies in your isSatisfied method. You do not perform any bounds checking to make sure that you only access valid options in your array. You should never try to access values outside the bounds of your array. When you do, you receive an ArrayIndexOutOfBounds exception. This is what is happening in your case. As the comments on your question suggested, you should investigate bounds checking further so that you understand the underlying issue here.

Your method should be changed to something like:

public static boolean isSatisfied(char[][] tissue, int row, int col, int threshold){
int total = 0;
int same = 0;
if(tissue[row][col] == 'X'){
    total = 0;
        if(col-1 >= 0 && row+1 < tissue.length && tissue[row + 1][col - 1] == 'X'){
            same ++;
            total ++;
        }else if(col-1 >= 0 && row+1 < tissue.length && tissue[row + 1][col - 1] == 'O')
            total ++;
        if(row+1 < tissue.length && tissue[row + 1][col] == 'X'){
            same ++;
            total ++;
        }else if(row+1 < tissue.length && tissue[row + 1][col] == 'O')
            total ++;
        if(row+1 < tissue.length && col+1 < tissue[row+1].length && tissue[row + 1][col + 1] == 'X'){
            same ++;
            total ++;
        }else if(row+1 < tissue.length && col+1 < tissue[row+1].length && tissue[row + 1][col + 1] == 'O')
            total ++;
        if(col-1 >= 0 && tissue[row][col - 1] == 'X'){
            same ++;
            total ++;
        }else if(col-1 >= 0 && tissue[row][col - 1] == 'O')
            total ++;
        if(col+1 < tissue[row].length && tissue[row][col + 1] == 'X'){
            same ++;
            total ++;
        }else if(col+1 < tissue[row].length && tissue[row][col + 1] == 'O')
            total ++;
        if(row-1 >= 0 && col-1 >= 0 && tissue[row - 1][col - 1] == 'X'){
            same ++;
            total ++;
        }else if(row-1 >= 0 && col-1 >= 0 && tissue[row - 1][col - 1] == 'O')
            total ++;
        if(row-1 >= 0 && tissue[row - 1][col] == 'X'){
            same ++;
            total ++;
        }else if(row-1 >= 0 && tissue[row - 1][col] == 'O')
            total ++;
        if(row-1 >= 0 && col+1 < tissue[row-1].length && tissue[row - 1][col + 1] == 'X'){
            same ++;
            total ++;
        }else if(row-1 >= 0 && col+1 < tissue[row-1].length && tissue[row - 1][col + 1] == 'O')
            total ++;

}
if(tissue[row][col] == 'O'){
    total = 0;
        if(col-1 >= 0 && row+1 < tissue.length && tissue[row + 1][col - 1] == 'O'){
            same ++;
            total ++;
        }else if(col-1 >= 0 && row+1 < tissue.length && tissue[row + 1][col - 1] == 'X')
            total ++;
        if(row+1 < tissue.length && tissue[row + 1][col] == 'O'){
            same ++;
            total ++;
        }else if(row+1 < tissue.length && tissue[row + 1][col] == 'X')
            total ++;
        if(row+1 < tissue.length && col+1 < tissue[row+1].length && tissue[row + 1][col + 1] == 'O'){
            same ++;
            total ++;
        }else if(row+1 < tissue.length && col+1 < tissue[row+1].length && tissue[row + 1][col + 1] == 'X')
            total ++;
        if(col-1 >= 0 && tissue[row][col - 1] == 'O'){
            same ++;
            total ++;
        }else if(col-1 >= 0 && tissue[row][col - 1] == 'X')
            total ++;
        if(col+1 < tissue[row].length && tissue[row][col + 1] == 'O'){
            same ++;
            total ++;
        }else if(col+1 < tissue[row].length && tissue[row][col + 1] == 'X')
            total ++;
        if(row-1 >= 0 && col-1 >= 0 && tissue[row - 1][col - 1] == 'O'){
            same ++;
            total ++;
        }else if(row-1 >= 0 && col-1 >= 0 && tissue[row - 1][col - 1] == 'X')
            total ++;
        if(row-1 >= 0 && tissue[row - 1][col] == 'O'){
            same ++;
            total ++;
        }else if(row-1 >= 0 && tissue[row - 1][col] == 'X')
            total ++;
        if(row-1 >= 0 && col+1 < tissue[row-1].length && tissue[row - 1][col + 1] == 'O'){
            same ++;
            total ++;
        }else if(row-1 >= 0 && col+1 < tissue[row-1].length && tissue[row - 1][col + 1] == 'X')
            total ++;


}
if(tissue[row][col] == ' '){
return true;
}if(total == 0){
    return false;
    }else if(((same / total) * 100) >= threshold){
    return true;
}else{ return false;}
}       

Upvotes: 0

Related Questions