Im having trouble with 2.6 the array

Listing 2.6 Adding variables and a Question array (QuizActivity.java)

I’m having trouble with the following line of code:

//adding question array
private Question[] mQuestionBank = new Question[]{
 new Question(R.string.question_australia, true),
 new Question(R.string.question_oceans, true),
 new Question(R.string.question_mideast, false),
 new Question(R.string.question_africa, false),
 new Question(R.string.question_americas, true),
 new Question(R.string.question_asia, true),
 }; //end of question array*************
private int mCurrentIndex = 0; // index for array

The code provided looks fine.

What’s your trouble?
Please also provide file strings.xml under directory /res/values.

Im getting error that Question isn’t recognized. The Question [ ] isn’t recognized.

I will also show you my string files

Please also show your file Question.java.

String files below:

GeoQuiz
//12-7-17 9:52pm AR added code 6 lines
//questions
Canberra is the capital of Australia.
The Pacific Ocean is larger than the Atlantic Ocean.
The Suez Canal connects the Red Sea and Indian Ocean.
The source of the Nile River is in Egypt.
The Amazon River is the longest river in the Americas.
Lake Baikal is the world’s oldest and deepest freshwater lake.

//True,false, and next buttons
<string name="true_button">True</string>
<string name="false_button">False</string>
//12-7-17 9:52pm AR added code 1 line
<string name="next_button">Next</string>

//toast(bubble pop-up)
<string name="correct_toast">Correct!</string>
<string name="incorrect_toast">Incorrect! </string>

Question.java file:

package com.bignerdranch.android.geoquiz;

/**

  • Created by JB on 12/6/2017.
    */
    private int mTextResId;
    private boolean mAnswerTrue;

public class Question(int textResId, boolean answerTrue)
{
mTextResId = textResId;
mAnswerTrue = answerTrue;

public int getTextResId()
    {
        return mTextResId;
}

public void setTextResId(int textResId)
    {
        mTextResId = textResId;
    }

public boolean isAnswerTrue()
    {
        return mAnswerTrue;
    }

public void setAnswerTrue(boolean answerTrue)
    {
        mAnswerTrue = answerTrue;
    }

}

public class Question(int textResId, boolean answerTrue)  // this line is also incorrect
{
mTextResId = textResId;
mAnswerTrue = answerTrue;
}               //  <- I guess you lost the right brace here. Please check whether you have some compile error in file Question.java.

Let me paste the correct one here.


public class Question {     // class name without any parameter

    private int mTextResId;
    private boolean mAnswerTrue;

    public Question(int textResId, boolean answerTrue) {    // constructor
        mTextResId = textResId;
        mAnswerTrue = answerTrue;
    }                                       // constructor with right brace

    // ......
}

Thank you Crystal. I’ll give it a go and let you know the results.

I just wanted you to know its working perfectly. Thank you. I was able to find other smaller errors because of that 1 error.

My problem is not with the class Question but with the java array Question[]. Everything works correctly until I add a 2nd string to the array. In particular I get the error 'cannot resolve symbol question_oceans". I have checked many times and my code is identical with the text. Help will be greatly appreciated.
Stoo

Hi. I’m new here. You should post what you have so you can have a second set of eyes. I remember posting this problem but my code was incorrect and Crystal showed me my error.

My array problem had a simple solution. I was able to figure out the trouble was probably the R.java file. While searching on Google, there was a suggestion to rebuild. When I did that the R.java file was updated to give id numbers to the new array items I had added to the code. Then everything worked correctly.
Stoo

Ok I’m glad you rolling.

I think you should get some time to learn core java, and after that come back to android/ Good luck!

Good Luck to you too.