mardi 2 février 2016

Boolean doesn't break for statement

I am new to the forum AND new to programming. I apologize in advance if my questions are really basic but I am really new to this and I had to learn a lot in a really short time, so I may miss a couple of concepts.

On to my question, I have a method who is supposed to check if a matrix still have spaces available and stop if it finds one.

The matrix has been initialized like this (I know it could be in one line, but the matrix is created within the constructor of an object)

protected char[][] matJeu = null;
matJeu = new char[10][10];

It has then been filled with spaces like this ' ' with a for statement and it works just fine

Here is the method

public boolean checkIfFull()
{
    boolean full = true;
    for (int i=0;i<matJeu.length || !full;i++)
    {
        for (int j=0;j<matJeu[i].length || !full ;j++)
        {
            if(matJeu[i][j]==' '){
                full = false;
            }
        }
    }
    return full;
} 

The problem is that when the full boolean turns to false, the for loops doesn't break and ends up by causing an ArrayOutOfBounds exception. If the matrix is full, it simply returns true. So I may be missing the correct way to cause a boolean to break a for loop.

Thank you in advance for your time.

Aucun commentaire:

Enregistrer un commentaire