'Assigning a name from an array list to a group

Hi guys this is really stumping me, I need to assign the names from the array list to the group a or b, my question is if I can just randomly select the names rather than randomly selecting group A or B

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Random;
import java.util.ArrayList;

public class GroupPicker{

    public static void main(String[] args) throws FileNotFoundException {

        Scanner input = new Scanner(new File("GroupPicker1.txt"));
        ArrayList<String> names = new ArrayList<String>();
        while (input.hasNext()) {
            names.add(input.next());
            String group = "";
            String getgroup = assignGroup(group);
            for (String n: names) {
                System.out.println(n + " is assinged to " + getgroup);
            }
        }

        System.out.println( "\n" + "project groups: ");
        System.out.println("Group A: ");
        System.out.println("Group B: ");
    }

    public static String assignGroup(String group) {
        Random rand = new Random();
        int groupnumber;
        groupnumber = rand.nextInt(2) + 1;

        switch(groupnumber) {
            case 1:
                group = "group B";
                break;
            case 2:
                group = "group A";
        }

        return group;
    }

}


Solution 1:[1]

Extract the loading of the names in a separate method:

private static List<String> loadNames() throws FileNotFoundException {
    Scanner input = new Scanner(new File("GroupPicker1.txt"));

    ArrayList<String> names = new ArrayList<>();
    while (input.hasNext()) {
        names.add(input.next());
    }

    return names;
}
  • Create 2 lists, one for each group.
  • Then go through the names list and randomly assign each of them to a list;

(If you have only 2 groups, then you can use rand.nextBoolean())

public static void main(String[] args) throws FileNotFoundException {

    List<String> names = loadNames();

    ArrayList<String> groupA = new ArrayList<>();
    ArrayList<String> groupB = new ArrayList<>();

    Random rand = new Random();
    for (String n: names) {
        if (rand.nextBoolean()) {
            System.out.println(n + " is assigned to group A");
            groupA.add(n);
        } else {
            System.out.println(n + " is assigned to group B");
            groupB.add(n);
        }
    }

    System.out.println();
    System.out.println("project groups: ");
    System.out.println("Group A: " + groupA);
    System.out.println("Group B: " + groupB);
}

There are ways to make it lot nicer using Streams if you want

EDIT: A Stream version

public static void main(String[] args) throws IOException {
    Random rand = new Random();

    Map<Boolean, List<String>> groups = Files.lines(Paths.get("GroupPicker1.txt"))
            .collect(Collectors.partitioningBy(x -> rand.nextBoolean()));

    System.out.println();
    System.out.println("project groups: ");
    System.out.println("Group A: " + groups.get(true));
    System.out.println("Group B: " + groups.get(false));
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1