0

I recently started with android programming, with just basic knowledge in java. I'm having trouble with my code,What I'm aiming is to display a randomly chosen text that's already programmed in my array after the button is clicked (onclick event).

public void magicbegins() //
{
    int min = 0;
    int max = 3;
    Random r = new Random();
    int rand = r.nextInt(max - min + 1) + min;
    //generating random number from 0 to 3 to use as index in later event
    String[] magictext = {"yes", "no", "maybe"};

    TextView text = (TextView) findViewById(R.id.textView1);
    //using the generated number as index for programmed string array
    text.setText(magictext[rand]);
}

If any case this codes is not recommendable to use, will anyone provide a sample script that would do similar from what I aim at very least?

2
  • 1
    You'll want int rand = r.nextInt( max - min ) + min;. In this specific case, you can just use rand = r.nextInt( max ); Commented Mar 19, 2013 at 15:11
  • +1, when min is 0, it serves no purpose to add/subtract it anywhere. That being said, it works in general Commented Mar 19, 2013 at 15:12

3 Answers 3

5

Since your index needs to be 0, 1, or 2, just use r.nextInt(3) (or, if you reorder the variable declarations, r.nextInt(magictext.length)). You certainly should not be using r.nextInt(max - min + 1) since that will occasionally give 3, which is an out-of-bounds index.

This formula:

r.nextInt(max - min + 1) + min

is appropriate when min and max both need to be included in the generated range of random integers. When the desired range is up to, but not including, max, the formula should be:

r.nextInt(max - min) + min

My suggestion is to use this, but with 0 and 3 substituted for min and max, respectively.

You might also consider moving magictext and r out of the method and make them member fields of the class. You can do the same thing with the text field, so you don't need to be looking it up each time. You can initialize the text field in your onCreate method. Your code would then look like this:

private final Random r = new Random();
private final String[] magictext = {"yes", "no", "maybe"};
private TextView text;

protected void onCreate(Bundle savedInstanceState) {
    . . . // what you have now, followed by
    text = (TextView) findViewById(R.id.textView1);
}

public void magicbegins()
{
    int rand = r.nextInt(magictext.length);

    text.setText(magictext[rand]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Couldn't have said it better myself
ahh thank you, it must be the random integer that causes the program failure at time,.. thank you accepted as answer
1

As @Ted Hopp Suggested

use this

public void magicbegins() 
{
    Random r = new Random();
    int rand = r.nextInt(3);
    String[] magictext = {"yes", "no", "maybe"};

    TextView text = (TextView) findViewById(R.id.textView1);

    text.setText(magictext[rand]);
}

2 Comments

As He is new so I thing full code is good for him..Sorry about that
I just use its code in actual code what Hobo wants and Ted changed his code later..
0

Random#nextInt(int n)

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The general contract of nextInt is that one int value in the specified range is pseudorandomly generated and returned. All n possible int values are produced with (approximately) equal probability.

int rand = r.nextInt(max - min + 1) + min;

which can generate 3 .Since your array has only 3 elements its index will be 0,1,2. Trying to access magictext[3] will cause ArrayIndexOutOfBoundsException.

1 Comment

That's not an answer, just a restatement of the problem.

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.