1

in my app in swift I have this code:

struct Question
{
    var QuestionLbl : String!
    var Answers : [String]!
    var Answer : Int!
}

After that I use this struct like this

var Questions = [Question]()

Questions =
            [Question(QuestionLbl: "Whats my name?", Answers: ["John","Josh","Adam","Leo"], Answer: 0),

                Question(QuestionLbl: "Whats my moms name?", Answers: ["Jessica","Crystal","Samanta","Kate"], Answer: 3),

                Question(QuestionLbl: "Whats my fathers name?", Answers: ["Ed","Blake","Jeff","Jonhson"], Answer: 2)]

Now, I'm trying to make the same app for Android.. So I created a class and tried to do the same thing like that.. This is the class file:

public class Question {
    String questionLabel;
    String[] answersOptions;
    Integer correctAnswer;

    public Question(String questionLabel, String[] answersOptions, Integer correctAnswer) {
        this.questionLabel = questionLabel;
        this.answersOptions = answersOptions;
        this.correctAnswer = correctAnswer;
    }

    public String getquestionLabel() {
        return questionLabel;
    }

    public String[] getanswersOptions() {
        return answersOptions;
    }

    public Integer getcorrectAnswer() {
        return correctAnswer;
    }
}

and this is how I try to do in main activity:

Question[] questions;
        questions = {
                 Question("Whats my name?",{"John","Josh","Adam","Leo"}, 1),
                 Question("Whats my mom's name?",{"Jessica","Crystal","Samanta","Kate"}, 1)
        };

But it didn't work. Whats is the problem?

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253

1 Answers1

2

Look at this post for the syntax of declaring arrays. Essentially, it'd look something like:

questions = new Question[]{
    new Question("What's my name?", new String[]{"John","Josh","Adam","Leo"}, 1),
    new Question("What's my mom's name?", new String[]{"Jessica","Crystal","Samanta","Kate"}, 1)
    //etc..
}
Community
  • 1
  • 1
Michael
  • 2,500
  • 1
  • 13
  • 26