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.

3
  • Possible duplicate of Syntax for creating a two-dimensional array Commented Nov 4, 2015 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. Commented Nov 4, 2015 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. Commented Nov 4, 2015 at 12:55

1 Answer 1

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];
}
Sign up to request clarification or add additional context in comments.

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.