1

I'm writing code for a small monster game based on 2DArray. But I can't proceed further until ArrayStoreException get handled. I want to do the following task by using java.util.Arrays & Arrays.fill. Don't suggest another way. Just want to resolve this. Any help would be highly appreciated. Thanks in advance....

package PlayWithStars;    
import java.util.Arrays;

public class Monster {
    static char battleBoard[][] = new char[10][10];

    public void buildBattleBoard()  {
        for (char[] row : battleBoard)    {
            Arrays.fill(battleBoard,'*');
        }
    }

    public void redrawBoard() {               
        for (int k=1 ; k<=30 ; k++) {         // 
            System.out.print("-");            // to print  ------------------
        }                                     //

        System.out.println();

        for (int i = 0; i < battleBoard.length; i++) {           
            for (int j = 0; j < battleBoard[i].length; j++) {   
                System.out.println("|"+battleBoard[i][j]+"|");   
            }                                                    
            System.out.println("");                           


        for (int k=1 ; k<=30 ; k++) {        //
            System.out.print("-");           // to print  ------------------
        }                                    //
    }

    public static void main(String[] args) {
        Monster m = new Monster();
        m.buildBattleBoard();
    }
}
2
  • Can you add stack trace for the error you get. Commented Jan 20, 2014 at 6:08
  • 3
    Arrays.fill(battleBoard,''); should be Arrays.fill(row,''); and redraw() is not at all called Commented Jan 20, 2014 at 6:09

3 Answers 3

5

in buildBattleBoard(), Arrays.fill(battleBoard,'*'); should be Arrays.fill(row,'*');. You forgot to reference the array in the for-each loop.

The compiler is using the Arrays.fill( Object[] , Object); method instead of the Arrays.fill( char[] , char);, hence no errors during compile time.

Sign up to request clarification or add additional context in comments.

Comments

0

Array.fill expects a dimensional array and you are providing nothing.

int i=0;
    for (char[] row : battleBoard)    {
           Arrays.fill(battleBoard[i],'*');
           i++;
      }

Comments

0
Arrays.fill(battleBoard, '*'); 

Replace the above line of code by:

Arrays.fill(row, '*');

And everything will work as expected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.