-1

It says here that length = 1 and index = 1 and im getting an error at line 28 but i started the array with 0. Link

public class Questions {
    public String mQuestions[] = {
            "1 + 1",
            "2 + 2",
            "4 + 4",
            "3 + 3",
    };
    private String mChoices[][] = {
            {"324, 231, 2"},
            {"456, 4, 2461"},
            {"8, 72, 45"},
            {"34, 6, 531"}
    };
    private String mCorrectAnswers[] = {
            "2,", "4,", "8", "6",
    };
    public String getQuestion(int a){
        String question = mQuestions[a];
        return question;
    }
    public String getChoice1(int a){
        String choice = mChoices[a][0];
        return choice;
    }
    public String getChoice2(int a){
        String choice = mChoices[a][1];
        return choice;
    }
    public String getChoice3(int a){
        String choice = mChoices[a][2];
        return choice;
    }
    public String getCorrectAnswer(int a){
        String answer = mCorrectAnswers[a];
        return answer;
    }
}

StackedQ
  • 3,999
  • 1
  • 27
  • 41
Newbie
  • 19
  • 4

2 Answers2

2

Correct the initialization for mChoices[][] as follows

private String mChoices[][] = {
    { "324", "231", "2" },
    { "456", "4", "2461" },
    { "8", "72", "45" },
    { "34", "6", "531" }};

Reason : {"324, 231, 2"} is a single string where { "324", "231", "2" } is three strings

1

Your array declaration is incorrect. This

private String mChoices[][] = {
        { "324, 231, 2" },
        { "456, 4, 2461" },
        { "8, 72, 45" },
        { "34, 6, 531" }
};

should be

private String mChoices[][] = {
        { "324", "231", "2" },
        { "456", "4", "2461" },
        { "8", "72", "45" },
        { "34", "6", "531" }
};

As is, your array only has a single dimension with any values. Although, it might look a bit odd at first glance, you could also write it like

private String mChoices[][] = { 
        "324, 231, 2".split("\\s*,\\s*"),
        "456, 4, 2461".split("\\s*,\\s*"),
        "8, 72, 45".split("\\s*,\\s*"),
        "34, 6, 531".split("\\s*,\\s*") 
};
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249