getting duplicate array output - java

Posted by dowln on Stack Overflow See other posts from Stack Overflow or by dowln
Published on 2010-06-16T15:04:26Z Indexed on 2010/06/16 15:12 UTC
Read the original article Hit count: 107

Filed under:
|

Hello,

Can someone could be kind and help me out here. Thanks in advance...

My code below outputs the string as duplicates. I don't want to use Sets or ArrayList. I am using java.util.Random. I am trying to write a code that checks if string has already been randomly outputted and if it does, then it won't display. Where I am going wrong and how do I fix this.

public class Worldcountries
{

    private static Random nums = new Random();   

    private static String[] countries =
    {
        "America", "Candada", "Chile", "Argentina"
    };


    public static int Dice()
    { 
        return (generator.nums.nextInt(6) + 1);  
    } 


    public String randomCounties()
    {
        String aTemp = " ";
        int numOfTimes = Dice();
        int dup = 0;

        for(int i=0 ; i<numOfTimes; i++)
        {
            // I think it's in the if statement where I am going wrong. 
            if (!countries[i].equals(countries[i])) 
            {
                i = i + 1;
            }
            else
            {
                dup--;  
            }

            // and maybe here  
            aTemp = aTemp + countries[nums.nextInt(countries.length)];
            aTemp = aTemp + ",";  
        }

        return aTemp;
    }
}

So the output I am getting (randomly) is, "America, America, Chile" when it should be "America, Chile".

© Stack Overflow or respective owner

Related posts about java

Related posts about homework