IIMON
IIMON

Reputation: 5

How to Display a Random Word out of a Set?

I'm extremely new to Java, but decided to create a simple game. When you press a button, the program "flips a coin" and displays either "True" or "False".

The idea is that based on a random generated number, it will either display "True" or "False".

This is my current code:

package com.me.koteg.dmaker;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void generate(View view) {

    final TextView textOne = (TextView) findViewById(R.id.txtDisplay);
    Button pushMe = (Button) findViewById(R.id.button);

    final String[] myCoin= {"Heads", "Tails"};

    Random rand = new Random();

    int number = rand.nextInt(3);

pushMe.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        textOne.setText(myCoin[3]);
    }
});

But when ever I attempt to run I get an error:

01-05 18:26:58.644 22736-22736/com.me.koteg.dmaker E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.me.koteg.dmaker, PID: 22736
 java.lang.ArrayIndexOutOfBoundsException: length=2; index=3
     at com.me.koteg.dmaker.MainActivity$1.onClick(MainActivity.java:34)
     at android.view.View.performClick(View.java:5204)
     at android.view.View$PerformClick.run(View.java:21153)
     at android.os.Handler.handleCallback(Handler.java:739)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:148)
     at android.app.ActivityThread.main(ActivityThread.java:5417)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I have a feeling that I might have wrote the code completely wrong, but any suggestions/help from you guys would be great!

UPDATE: Thanks for all the help, from you guys. I didn't think that I'd get help so quickly, especially after looking to the android forums and not even getting a thorough response. As most of you pointed out, it was the number that I had set in the Random generator.

Upvotes: 0

Views: 104

Answers (3)

Elliott Frisch
Elliott Frisch

Reputation: 201447

You should be accessing a random coin (not trying to get one from an illegal hard-coded index, 3, in your array). Also, I would use the array length as the argument to rand.nextInt() (that way, for example, you might switch to dice by changing the array contents alone).

Random rand = new Random();
pushMe.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        int number = rand.nextInt(myCoin.length);
        textOne.setText(myCoin[number]);
    }
});

Upvotes: 0

George Mulligan
George Mulligan

Reputation: 11903

Do you want a new random number on each press? Your code is close. When you get a random number using nextInt it gets a number between 0 inclusive and what you pass in exclusive.

Since your array only has two elements you want to get either 0 or 1 and thus need to pass in 2. You then need to index into your array using that random number instead of hard coding a number.

The original code is failing because you specified an invalid index into your array. As stated in the exception it only has a length 2 and you specified index 3 which is invalid since arrays are indexed starting at 0.

pushMe.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        int number = rand.nextInt(2);
        textOne.setText(myCoin[number]);
    }
});

Upvotes: 1

Fred
Fred

Reputation: 1486

The problem arises when you try to index your array of myCoin, as you can see the exception was "ArrayIndexOutOfBoundsException" which indicates you have somewhere an array access which attempts to modify an array element outside the bounds of the array. There are only two elements in this set and these sets are 0 indexed meaning that your max index is 1, so you want a random number between 0 and 1 to index your coin.

public void generate(View view) {

    final TextView textOne = (TextView) findViewById(R.id.txtDisplay);
    Button pushMe = (Button) findViewById(R.id.button);

    final String[] myCoin= {"Heads", "Tails"};

    Random rand = new Random();

    int number = rand.nextInt(2);

pushMe.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        textOne.setText(myCoin[number]);
    }
});
}

Upvotes: 0

Related Questions