I've tried this myself and have looked everywhere for an answer and can't seem to find a suitable workaround. I've found many answers on this site before so I've signed up to ask my question. I have a program that loops through 6 users and assign them to a position. Users: Alpha, bravo, charlie, delta, echo and frank. Positions: 1,2,3,4,5,6. I'm generating random numbers to assign the user to their position. I have one rule that I am checking for; no user can be assigned to the same position twice. Because of this rule and the random assignment of the users to stations, sometimes the only user left for position 6 is a user that has already been assigned to position 6 in a previous rotation. When this happens my program crashes and rightfully so. How do I overcome this? I've had instances where the program will make the right choices and I have 6 successful rotations. I would like to have 6 successful rotations every time. I have spent two week trying to figure this out. Any help I can get would be greatly appreciated. Thank you. I've pasted my code below. I'm using Java.
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.CardLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
public class test extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test frame = new test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
//TODO
int randnum[] = new int [7];
int numOfRotations = 1;
String[] String_CurrentOperator = new String[7];
Random rand_opers = new Random();
List<Integer> ArrayList_UsedRandNums = new ArrayList<>();
List<List<String>> ArrayList_MainOperatorHistory = new ArrayList<List<String>>();
List<String> ArrayList_AllOpers = new ArrayList<>();
List<String> ArrayList_UsedOpers = new ArrayList<>();
List<String> ArrayList_OperatorHistory1 = new ArrayList<>();
List<String> ArrayList_OperatorHistory2 = new ArrayList<>();
List<String> ArrayList_OperatorHistory3 = new ArrayList<>();
List<String> ArrayList_OperatorHistory4 = new ArrayList<>();
List<String> ArrayList_OperatorHistory5 = new ArrayList<>();
List<String> ArrayList_OperatorHistory6 = new ArrayList<>();
public test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBackground(Color.DARK_GRAY);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new CardLayout(0, 0));
JButton btnShuffle = new JButton("SHUFFLE");
contentPane.add(btnShuffle, "name_253561263644851");
btnShuffle.addActionListener(this);
ArrayList_AllOpers.add("POSITION ZERO");
ArrayList_AllOpers.add("Alpha");
ArrayList_AllOpers.add("Bravo");
ArrayList_AllOpers.add("Charlie");
ArrayList_AllOpers.add("Delta");
ArrayList_AllOpers.add("Echo");
ArrayList_AllOpers.add("Frank");
ArrayList_MainOperatorHistory.add(ArrayList_OperatorHistory1);
ArrayList_MainOperatorHistory.add(ArrayList_OperatorHistory1);
ArrayList_MainOperatorHistory.add(ArrayList_OperatorHistory2);
ArrayList_MainOperatorHistory.add(ArrayList_OperatorHistory3);
ArrayList_MainOperatorHistory.add(ArrayList_OperatorHistory4);
ArrayList_MainOperatorHistory.add(ArrayList_OperatorHistory5);
ArrayList_MainOperatorHistory.add(ArrayList_OperatorHistory6);
}
public void actionPerformed(ActionEvent arg0) {
/*******************************
*****ASSIGN ALL OPERATORS******
*******************************/
System.out.println("THIS IS ROTATION "+numOfRotations);
genRandoms();
for(int i=1; i<6+1; i++){
do{
//randnum[i] = rand_opers.nextInt(6)+1;
String_CurrentOperator[i] = ArrayList_AllOpers.get(randnum[i]);
}
while(ArrayList_UsedOpers.contains(String_CurrentOperator[i]) || ArrayList_MainOperatorHistory.get(i).contains(String_CurrentOperator[i]));
ArrayList_UsedOpers.add(String_CurrentOperator[i]); //add to used names thus far
}
for(int i=1; i<6+1; i++){
System.out.println(String_CurrentOperator[i]); //Prints the name of the operator working on the station
ArrayList_MainOperatorHistory.get(i).add(String_CurrentOperator[i]); //adds operator to list of all users who have been assigned to this station
}
//Perform cleanup actions for next iteration of the loop
numOfRotations++; //increment the rotation count by 1
ArrayList_UsedRandNums.clear(); //clear the list of randum numbers used
ArrayList_UsedOpers.clear(); //clear the list of assigned operators
System.out.println("");
}
public void genRandoms(){
for(int i=1; i<6+1; i++){
do{
randnum[i] = rand_opers.nextInt(6)+1;
}
while (ArrayList_UsedRandNums.contains(randnum[i]));
ArrayList_UsedRandNums.add(randnum[i]); //add randnum[i] to list of random numbers used thus far
}
}
}