0

Hey all - In Android, I wrote a number of strings in my string.xml I would like to display on a TextView based on a random number...Here is what I have:

  int randCropPercentage = (int) Math.ceil(Math.random() * 100);  
  Random randPhrase50 = new Random();
        int[] array50 = new int[] { R.string.ss2, R.string.ss4, R.string.ss5,
                R.string.st4, R.string.st5, R.string.tt2, R.string.tt3,
                R.string.tt5, R.string.to2, R.string.to3, R.string.to4,
                R.string.os5 };
        int randPhrase = randPhrase50.nextInt(array50.length - 1);

Inside an if statement, I have this:

 if (randomCropPercentage < 50){
                mTheMessage.setText(array50(randPhrase));
                            //etc

But I know I am not doing it right because I get the error:

The method array50(int) is undefined for the type MAIN

Any ideas?

2 Answers 2

3

The fact is you should write this:

array50[randPhrase]

Arrays give the access to elements not via (), but via []

Sign up to request clarification or add additional context in comments.

Comments

2

Accessing an array is with []

Try this : mTheMessage.setText(array50[randPhrase]);

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.