Java- Iterating backwards through Array under a condition

So for one of my game models, there is an array of elements represented as a string "--X-X" so that this board has 5 boxes, and positions are 0-4. Each 'X' can only move left. There is an "X-Index" so that if I run getIXPosition(1) it will return the position of the first X which is 2. And getXPosition(2) will return second X's position which is 4. So the string board is "--X-X" but in my code it's represented as an array as "00102" so that I can keep track of xIndex.


Now my issue is that I need to make a move method that prevents the second x from skipping over the first X into position 1 or 0. That is not allowed in this game. I thought I wrote the method correctly but my tests aren't passing when I test to make sure second X can not hop over any X's before it.



public void move(int xIndex, int newPosition) {
int oldPosition = getXPosition(xIndex);

for(int i= oldPosition - 1; i >= 0;i--) {
while (board[i] == 0 ) {
board[oldPosition] = '0'; // so this spot is open '-'
board[newPosition] = xIndex;
}

throw new IllegalArgumentException("Error cannot move X to new position");
}
}


What am I doing wrong?