Hello,
When random generates an output, it cannot show duplicate strings. So for example the output I am getting (randomly) is, "Freddy, Freddy, Jane" when it should be "Freddy, Jane". I have to hardore so I cannot use Sets or ArrayList, Contains or for anything like that. Also no Stringbuilder. I don't know how to fix this. Does anybody know to kindly help me. Thanks in advance
java.util.Random.
public class frogs
{
private static Random nums = new Random();
private static String[] names =
{
"Freddy", "Mac", "Jane", "Jule"
};
public static int Dice()
{
return (names.nums.nextInt(2) + 1);
}
public String randomNames()
{
String tempString = " ";
int numOfTimes = Dice();
int dup = 0;
for(int i=0 ; i<numOfTimes; i++)
{
if (names[i].equals(names[i])) [I]// if two strings are the same then it should
//not output the string again.
// I know this loop is wrong but I don't how to
//fix this [/I]
{
i = 0;
}
tempString = tempString + names[nums.nextInt(names.length)];
tempString = tempString + ",";
}
return aTemp;
}
}
}