2

I'm new to coding and need to make a Yathzee game. I want to initialize only one column in my 2D-array, but can't find how to.

players = p;
    String[][] rnc= new String[21][p+1];

    for(int i = 0; i < 20; i++){
        for(int j = 0; j < p + 1; j++){
            rnc[i][0] = {"upper section", "ones", "twos", "threes", "fours", "fives", "sixes", "total", "bonus", "total w bonus", "lower section", 
                    "3 of a kind", "4 of a kind", "full house", "small straight", "large straight", "YATHZEE", "chance", 
                    "total lower section", "total upper section", "grand total"};

This gets an "Array constants can only be used in initializers"-error.

Kyozoku
  • 29
  • 7
  • Possible duplicate of [Syntax for creating a two-dimensional array](http://stackoverflow.com/questions/12231453/syntax-for-creating-a-two-dimensional-array) – MiKE Nov 04 '15 at 12:21
  • Why are you using a looop with `j` counter if you never use `j` ? Also `rnc[i][0]` expects a `String` not an array. – Gaël J Nov 04 '15 at 12:39
  • I was going to use j later, but I'm going to do some modifications and I think the answer I got will work fine. – Kyozoku Nov 04 '15 at 12:55

1 Answers1

1

You could use a helper array :

String[] helper = {"upper section", "ones", "twos", "threes", "fours", "fives", "sixes", "total", "bonus", "total w bonus", "lower section", 
                "3 of a kind", "4 of a kind", "full house", "small straight", "large straight", "YATHZEE", "chance", 
                "total lower section", "total upper section", "grand total"};

for(int i = 0; i < rnc.length && i < helper.length; i++) {
    rnc[i][0] = helper[i];
}
Eran
  • 387,369
  • 54
  • 702
  • 768