Kurt
Kurt

Reputation: 777

Not taking the right value from an array

I want to define an array which will contain a bunch of strings.

I then want to be able to reference different strings within the array. Sounds simple but with the following code, I am returning "to be" (i.e. the 2nd string in the array) for the variable onOneClick (which should be referring to the 1st string or "to implicate".

Any ideas?

String hintsList[] = {"to implicate", "to be", "to bite", "to draw", "to run", "to go", "to escape", "to fall", "to accept", "to open", "to laugh", "to listen", "to open", "to dance", "to use", "to save (not waste)", "to create"};

final String onOneClick = hintsList[1];
final EditText box0101 = (EditText)findViewById(R.id.box0101);

        box0101.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            txtHint.setText(onOneClick);                
        }
    });

Upvotes: 0

Views: 80

Answers (2)

hall.stephenk
hall.stephenk

Reputation: 1165

Arrays in Java are zero-based. You should use final String onOneClick = hintsList[0];.

Upvotes: 1

Alex
Alex

Reputation: 25613

Arrays are indexed starting from zero. If you want the first element, use hintsList[0]

Upvotes: 7

Related Questions