1

Been trying to wrap my head around this logic, maybe you guys can point me in the right direction.

I have two String[], the one contains the Question Options, the other whether the option is correct or not.

Example:

String question = "Which of the following are fruit";

String[] questionOptions = "Mangos-Apples-Potatoes-Bananas".split("-");

String[] questionOptionsCorrect = "Y-Y-N-Y".split("-");

I am passing a List to my webservice, where each answerObject contains an option, and whether it is the correct option.

Example:

List< AnswerObjects > optionList = new ArrayList< AnswerObjects >();

answerObject.setAnswerText(Mangos);

answerObject.setAnswerCorrect(Y);

optionList.add(answerObject);

So my question is, How would I loop through the arrays and assign the right option and optionCorrect to each object.

Thanks anyone who's willing to help out.

1
  • 1
    Perhaps start by thinking about how to create the AnswerObject objects, and how to put them in the list. That might lead you to the next step. Commented Oct 4, 2013 at 12:44

2 Answers 2

2

Assuming your question array and answer array are parllel

 for(int i = 0 ; i< questionOptions.length;i++)
    {
        AnswerObject answerObject = new AnswerObject();
         answerObject.setAnswerText(questionOptions[i]);

        answerObject.setAnswerCorrect(questionOptionsCorrect[i]);
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Since you have two "parallel" arrays, you can loop on the index of one of them, and use the index for both:

if (questionOptionsCorrect.length != questionOptions.length) {
    // Throw an exception here: the arrays must have the same length
    // for the code below to work
}
for (int i = 0 ; i != questionOptionsCorrect.length ; i++) {
    AnswerObjects ans = new AnswerObjects();
    ans.setAnswerText(questionOptions[i]);
    ans.setAnswerCorrect(questionOptionsCorrect[i]);
}

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.