-3

I want to create an array of objects as mentioned in this answer:

Creating an array of objects in Java

i.e initialize one at a time like

A[] a = new A[4];    
a[0] = new A();

My class definition:

public class Question {
    private int mAnswer;
    private String mQuestion;
    private String[] mOptions;

    public Question(String question, int answer, String[] option){
        mQuestion = question;
        mAnswer = answer;
        mOptions = option;
    }

    public int getmAnswer() {
        return mAnswer;
    }

    public String getmQuestion() {
        return mQuestion;
    }

    public String[] getmOptions() {
        return mOptions;
    }
};

My Java Activity:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import static android.R.attr.x;

public class HomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
    }
    String[] options1 = {"ABCD", "EFGH", "IJKL", "MNOP"};
    String[] options2 = {"Bangalore", "Delhi", "Mumbai", "Pune"};
    String[] options3 = {"Business", "Work", "Nothing", "Study"};

    Question[] ques = new Question[3];

    ques[0] = new Question("What is your name?", 2, options1);

    ques[1] = new Question("Where are you from?", 3, options2);

    ques[2] = new Question("What do you do?", 4, options3);

}

But the initializations :

    ques[0] = new Question("What is your name?", 2, options1);

    ques[1] = new Question("Where are you from?", 3, options2);

    ques[2] = new Question("What do you do?", 4, options3);

give me an error. These lines get underlined RED giving me all of the following errors for each line:

-Identifier expected

-Invalid method declaration; return type required

-Missing method body, or declare abstract

-Parameter expected

-Unexpected token

-Unknown class: 'ques'

I have no errors when I use:

Question[] ques = new Question[]{new Question("What is your Name?", 2, options1), new Question("Where are you from?", 3, options2),new Question("What do you do?", 4, options3)};

Why is that happening?

I have no idea where I am going wrong. I'm a beginner in Android and Java. Any help would be appreciated. Thanks.

Ravi
  • 30,829
  • 42
  • 119
  • 173
apps
  • 25
  • 6
  • 2
    You have syntax error. You close your function after setContentView. Place closing bracket after ques[2] = .. – Vigen Jan 13 '18 at 14:49
  • 1
    Generally if you are going to put objects into a list you should be using an ArrayList not a static array like you are showing. – Andy Lebowitz Jan 13 '18 at 14:58

3 Answers3

1

You can't define below lines anywhere in class instead should be moved to some method

ques[0] = new Question("What is your name?", 2, options1);
ques[1] = new Question("Where are you from?", 3, options2);
ques[2] = new Question("What do you do?", 4, options3);

You probably can have a method name getQuestions move it inside

public Question[] getQuestions ()
{
    String[] options1 = {"ABCD", "EFGH", "IJKL", "MNOP"};
    String[] options2 = {"Bangalore", "Delhi", "Mumbai", "Pune"};
    String[] options3 = {"Business", "Work", "Nothing", "Study"};

    Question[] ques = new Question[3];

    ques[0] = new Question("What is your name?", 2, options1);

    ques[1] = new Question("Where are you from?", 3, options2);

    ques[2] = new Question("What do you do?", 4, options3);

    return ques;
}
Ravi
  • 30,829
  • 42
  • 119
  • 173
0

So you are using a static array instead of an ArrayList which you should be using to store objects (a string can go in either, but it's easier in an ArrayList). The main difference besides being used for objects is ArrayLists are also dynamic (objects can be added or removed).

Do the following:

ArrayList<Question> list = new ArrayList<Question>();
list.add(new Question("What is your name?", 2, options1));
list.add(new Question("Where are you from?", 3, options2));
list.add(new Question("What do you do?", 4, options3));

To access the objects in the arrayList (the 0th for example), simply do:

list.get(0);

The main issue here on the other hand though is that you are assigning values to your list in the class, and not within a method.

Make a method to use for assigning values to your list and I do not believe it will crash anymore. Something like what Ravi did above:

public Question[] getQuestions ()
{
// code for assigning the values that I wrote above.
}
Andy Lebowitz
  • 1,471
  • 2
  • 16
  • 23
-2

You should prefer Collections over arrays.

having sad this, you could use a different form of array initialization:

Question[] ques = new Question[/*no size here*/] { // curly brace insteadof semicolon    
    ques[0] = new Question("What is your name?", 2, options1) , // comma instead of semicolon    
    ques[1] = new Question("Where are you from?", 3, options2),
    ques[2] = new Question("What do you do?", 4, options3) // no comma at last entry
} // closing brace added
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51