Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
/**
 * Write a description of class RandomArray here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
import java.util.Scanner;
import java.util.Random;
public class RandomArray
{
   
    public RandomArray()
    {
        
        String[] array;
        array = new String[10];
        Scanner input = new Scanner(System.in);
        Random rand = new Random();
        int random = rand.nextInt(11);
        
        
        for (int i=0; i < (array.length); i++) {
            System.out.print("Enter a country: ");
            String country = input.next();
             if (array[random] == null)
        array[random] = country;
        else
        array[random] = array[random];
            
        }
        
       
        
         for (String country: array)
        System.out.print(country + ",");
Posted
Comments
Member 11190214 1-May-15 7:56am    
Also in the output I don't know how to show additionally which countries were entered in order.

1 solution

If I understand the question correct, you want the array with the countries to shuffeled. A solution would be to convert the array to an ArrayList and then use
Java
Collections.shuffle(ArrayList list);


Little example:
Java
public class ShuffleTest {
    public static void main(String[] args) throws Exception {
		String[] stringArray = { "A", "E", "I", "O", "U" };
		ArrayList<String> list = new ArrayList<String>();
		for(String s : stringArray)
			list.add(s);
		Collections.shuffle(list);
		String[] randomStringArray = list.toArray(new String[list.size()]);
		for(String s2 : randomStringArray)
			System.out.println(s2);
	}
}

First I create a String array with some values in it. After that I convert it to an ArrayList (There's probably a better way for converting array's to ArrayList's) and use Collections.shuffle(list); to shuffle it. If you need the string to be in an array instead of an ArrayList you can use the toArray() method to convert the ArrayList to an array.

Moritz
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900