-5
public class Elements {
    public static String element_answer = "";
    public String[] elements = new String[118];
    elements[0] = "Hydrogen";
    elements[1] = "Helium";
    elements[2] = "Lithium";
    elements[3] = "Beryllium";
    elements[4] = "Boron";
    (...)
    elements[117] = "Ununoctium";
}

I would like to randomly choose a element out of the array and assign the random choice to the variable element_answer. How could this be done?

hata
  • 11,633
  • 6
  • 46
  • 69

4 Answers4

1

Try this:

element_answer = elements[new Random().nextInt(elements.length)];
Harish Sridharan
  • 1,070
  • 6
  • 10
  • I then use p1_button1.setText(elementanswer); to assign it to the text of a button but nothing comes up on the button just NOTHING – user299824 Aug 03 '15 at 16:18
  • 1
    I would argue that creating a `new Random()` every time you want to get a new random element will be costly in the long run. I prefer @chayasan 's answer more, where `random` can be stored as an instance field if need be to avoid over reconstructing. – NoseKnowsAll Aug 03 '15 at 16:20
  • @user299824 Look carefully. You created an array with 113 null references. Your odds of choosing one of those 113 nulls instead of one of the 5 are rather high. I suggest you create your array a little differently. – dsh Aug 03 '15 at 17:00
  • @dsh OP have mentioned that all the 118 data have real entries in the comment section under the question itself. – Harish Sridharan Aug 03 '15 at 17:01
  • 1
    @user299824 you have to post the entire activity for me to point out where you are missing out. – Harish Sridharan Aug 03 '15 at 17:03
1

You can do something like this,

Random random = new Random();
element_answer = elements[random.nextInt(elements.length)];
Chaya Sandamali
  • 657
  • 9
  • 22
-1

You could use:

import java.util.Collections;
[...]
Collection.shuffle(elements);
element_answer = elements[0];

It would do the trick. You shuffle the whole list and take the first one which is now some element out of this list. Randomized by the Java Collections.

-1

I guess your intention with my ESP (joke) that A Class that returns a String which is randomly chosen from an array of Strings (which are names of chemical elements). If so, below is a possible implement.

Elements.java:

// The Class name of Java starts with upper case as normal
public class Elements {
    // There is no need of this field value.
    // public static String element_answer = "";

    public String[] elements = new String[118];

    // You need a random object
    private Random random = new Random();

    // Inside Constructor you can instantiate Strings in the Array
    public Elements() {
        // Array number starts from '0'.
        // This is quite basic knowledge. 
        elements[0] = "Hydrogen";
        elements[1] = "Helium";
        elements[2] = "Lithium";
        elements[3] = "Beryllium";
        elements[4] = "Boron";
        (...)
        elements[117] = "Ununoctium";
        // So array number starts from '0'
        // that the last number is [array length] - 1
    }

    // You need to define a method.
    // This is basic idea for Java Programmers!
    public String getElementRandom() {
        return elements[random.nextInt(elements.length)];
    }
}

Usage inside your Activity (or somewhere) is:

 // Declare at field
 Elements elements = new Elements();

 (...)

 // somewhere you want to use
 p1_button1.setText(elements.getElementRandom());
hata
  • 11,633
  • 6
  • 46
  • 69