How exactly do I create my code from selecting the same item twice from the array?
Example:
it'll print something like this "Gloria Gloria la Cinq Conflict"
How Do I stop it from doing that? Thanks in Advance
public class Main {
public static void main(String[] args) {
// The pump data consist of ONLY D24 PumpItUp charts
Random rand = new Random();
String[] PumpData = {"la Cinq", "Gloria", "Vanish", "Harma", "Conflict", "Sarabande", "Bedlam", "Final Audition", "Achluoias", "FFF", "Full moon", "Full moon FULL", "Annihilator" , "Creed FULL" , "BrainPower", "lolite", "Dement", "Destri", "Cross Soul", "TFTMN FULL", "Errorcode", "Dignity", "A Site De La Rue", "Trashy", "Paved Garden", "V3"};
for (int i = 0 ; i < 4; i ){
int rand_int = rand.nextInt(5);
String ChooseDeath = (PumpData[rand_int]);
System.out.println(ChooseDeath);
}
}
}
CodePudding user response:
You can maintain a set that keeps track of all chooseDeath String. Before printing it ,you can just verify whether the set contains it or not. The question arises , what do you want to do in case a duplicate occurs, do you want to still print 4 words or less words . I have shown the implementation which picks 4 words everytime.
public class SetTest {
public static void main(String[] args) {
// The pump data consist of ONLY D24 PumpItUp charts
Random rand = new Random();
String[] PumpData = {"la Cinq", "Gloria", "Vanish", "Harma", "Conflict", "Sarabande", "Bedlam", "Final Audition", "Achluoias", "FFF", "Full moon", "Full moon FULL", "Annihilator" , "Creed FULL" , "BrainPower", "lolite", "Dement", "Destri", "Cross Soul", "TFTMN FULL", "Errorcode", "Dignity", "A Site De La Rue", "Trashy", "Paved Garden", "V3"};
Set<String> set = new HashSet<>();
for (int i = 0 ; i < 4; i ){
int rand_int = rand.nextInt(5);
String chooseDeath = (PumpData[rand_int]);
if(!set.contains(chooseDeath))
{
set.add(chooseDeath);
System.out.println(chooseDeath);
}
else {
i--;
continue;
}
}
}
}
this will give the following output :
Conflict
Gloria
Harma
Vanish
CodePudding user response:
You need to keep track of what you've already selected. Something like:
Random rand = new Random();
Set<Integer> visited = new HashSet<>();
String[] PumpData = {...};
for(int i = 0; i < 4; i )
{
int rand_int = rand.nextInt(5);
while(visited.contains(rand_int))
{
rand_int = rand.nextInt(5);
}
System.out.println(PumpData[rand_int];
visited.add(rand_int);
}
CodePudding user response:
Plenty of answers already. I just want to add a solution using Streams.
new Random().ints(0, pumpData.length) // Create IntStream of random numbers between 0 and pumpData.length
.distinct() // Remove duplicates from the stream
.limit(4) // Limit stream to 4 numbers
.mapToObj(i -> pumpData[i]) // Map IntStream to Stream<String> using the words in the array
.forEach(System.out::println); // Print all the Strings
CodePudding user response:
You should store selected values in a Collection since collections has "is exists" or not function. I'd suggest you to store in a HashMap since it has O(1) access time or List.
after each drawing you can question whether I selected this value before or not.
