Random String from Array

How can I now return a random string from the following array?



import java.util.Random;

public class RpsBW {
    public void setObjects() {
        String objects[] = {"Rock", "Paper", "Scissors"};
    }
    public static void main(String args[]) {
        // main
    }
}

It would be decent to post an example, thanks! :smiley:

Try this:


import java.util.Random;


public class RpsBW
{
    public static void main(String[] args)
	{
        String[] objects = { "Rock", "Paper", "Scissors" };

        int length = objects.length;

        for (int i = 0; i < length; i++)
		{
            int rand = (int) (Math.random() * length);

            System.out.print(objects[rand]);

            System.out.print(" ");
        }
    }
}

Ok, that helped me a lot, thanks!

How can I display only one out of 3 random strings?

remove the loop