0

I'm trying to display a string inside a textView object. currently I'm only getting the first string in the string array.

   public void onClick(View v) {
        Resources res = getResources();
        String[] hintsStr = res.getStringArray(R.array.hints);
        TextView hintShow = (TextView)findViewById(R.id.hintShow);
        int rndInd = (int) Math.random()*5;
        //hintShow.setText(rndInd);
        hintShow.setText(hintsStr[rndInd]);
        //System.out.print(rndInd);
    }

3 Answers 3

2

Try this to generate your random numbers.-

Random rand = new Random();
int rndInd = rand.nextInt(5); // Will get a rand number between 0 and 4

Also, you should instantiate rand object just once, as an instance variable outside onClick method.

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

Comments

1

You are casting the result of Math.rand() to int so the result is always 0, you can do int rndInd = (int)(Math.random()*5); or even better use the solution that ssantos suggested

Comments

0

First initialize Random Class and then get value,see below code

Randon random=new Random();
int value = random.nextInt(10);

It will work for sure.

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.