0

So I am trying to create small Java program where names from array are paired. But I don't know how to proceed. I have array with names and the object is to pair two names randomly to make a team. There should be some statement so certain pairs couldn't be made: Miller & James can't be in the same team and no dublicates. How do I do this?

Example output: James & Hal

import java.util.Random;

public class Teams {

    public static void main (String [] args) {

        String [] arr = {"John", "James", "George", "Miller", "Hal", "Dan"};
        Random random = new Random();

         int select = random.nextInt(arr.length); 
         int selectSecond = random.nextInt(arr.length);

         System.out.println(arr[select]);
         System.out.println(arr[selectSecond]);      

         }
}
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
Ocelot
  • 9
  • 1
  • 6
    You need to explain those "statements" that prevent some pairing – jhamon Apr 17 '18 at 18:23
  • Pick two random names; if they are the same, or violate the constraints, pick another pair. – Andy Turner Apr 17 '18 at 18:23
  • 4
    Why can't Miller & James be in the same team? What does "no duplicates" mean? That one name can't be on two teams? Or that two different teams can't have the same members? Or that a team can't have the same name twice? Or something else? – Ted Hopp Apr 17 '18 at 18:23
  • @Andreas or maybe no [John, James] and [James, John] – jhamon Apr 17 '18 at 18:30

3 Answers3

0

I would like to use Collections.shuffle instead, and a do while loop like so :

String[] arr = {"John", "James", "George", "Miller", "Hal", "Dan"};
List<String> list = Arrays.asList(arr);
String name1, name2;
do {
    Collections.shuffle(list);
    name1 = list.get(0);
    name2 = list.get(1);
} while ((name2.equals("Miller") && name1.equals("James"))
        || (name1.equals("James") && name2.equals("Miller")));

System.out.println(String.format("%s & %s", name1, name2));

With this solution you don't need to check if the both names are same or not, you just need to check if the two name not equals in the same pair to Miller and James

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
0

You can delete the first chosen name from the array and then choose again to get the second one. To delete an element from an array see Removing an element from an Array (Java). Here is one possible implementation (but I didn't test it):

public static void main (String [] args) {

    String [] arr = {"John", "James", "George", "Miller", "Hal", "Dan"};
    Random random = new Random();

    int select = random.nextInt(arr.length);
    arr = removeElements(arr, arr[select]);
    int selectSecond = random.nextInt(arr.length);

    System.out.println(arr[select]);
    System.out.println(arr[selectSecond]);

}

// import java.util.LinkedList;
public static String[] removeElements(String[] input, String deleteMe) {
    List result = new LinkedList();

    for(String item : input)
        if(!deleteMe.equals(item))
            result.add(item);

    return (String[]) result.toArray(input);
}
Michael Osofsky
  • 11,429
  • 16
  • 68
  • 113
0

It will depend which perspective you want to attack here. If you just want to "do the job", you have an extensive list of possibilities (as we already have here), but I would just take care with readability:

public class Teams {

    private static String[][] teamsToAvoid = {{"James", "Miller"}, {"John", "Hal"}};
    private static String[][] teamsFormed = new String[3][2];

    public static void main(String[] args){
        String[] names = {"John", "James", "George", "Miller", "Hal", "Dan"};
        List<String> namesList = new ArrayList<>(Arrays.asList(names));
        Collections.shuffle(namesList);

        do {
            formTeam(namesList, 0, 1);
        } while(namesList != null && !namesList.isEmpty());

        for(String[] team : teamsFormed){
            System.out.println("Team: {" + team[0] + ", " + team[1] + "}");
        }
    }

    private static void formTeam(List<String> namesList, int firstPlayerIndex, int secondPlayerIndex) {
        if(isTeamPossible(namesList.get(firstPlayerIndex), namesList.get(secondPlayerIndex))){
            String firstPlayer = namesList.get(firstPlayerIndex);
            String secondPlayer = namesList.get(secondPlayerIndex);
            teamsFormed[getFormedTeamNextIndex()] = new String[]{firstPlayer, secondPlayer};
            namesList.remove(namesList.indexOf(firstPlayer));
            namesList.remove(namesList.indexOf(secondPlayer));
        } else {
            formTeam(namesList, firstPlayerIndex, ++secondPlayerIndex);
        }
    }

    private static boolean isTeamPossible(String player1, String player2) {
        for(String[] teamToAvoid : teamsToAvoid){
            if(Arrays.asList(teamToAvoid).contains(player1) && Arrays.asList(teamToAvoid).contains(player2)){
                return false;
            }
        }
        return true;
    }

    private static int getFormedTeamNextIndex() {
        for(int i = 0; i < teamsFormed.length; i++){
            if(teamsFormed[i][0] == null && teamsFormed[i][1] == null)
                return i;
        }
        return 0;
    }
}

Doing this you will prevent the same pair in different order and remove those players from the list (preventing their reuse).

I would pay attention when removing from list directly by index also, because when you remove one item the index for items after that one change.

Eduardo Meneses
  • 504
  • 3
  • 18