Steven Oh
Steven Oh

Reputation: 394

distrubuting people to groups evenly based on userinput

ReadFile randomGenerate = new ReadFile();
        Input userNum = new Input();
        String classnum = userNum.fileToSelect();
        int lineNumber = randomGenerate.lineCounter(classnum);

        //number of students in classnum.txt

        ArrayList<String> people = randomGenerate.readPeople();
        //a string of names(first word on each row)

        int userPut = userNum.numInput();
        int maxNum = lineNumber/userPut;

Right not, I am reading off a txt file - the first word of each row which is the name of a person. I want to distribute this ArrayList of people into even groups, with the number of groups based on userinput.

int[] numbers = RandomNumbersWithoutRepetition(0, lineNumber, lineNumber);       
//an array of numbers IN ORDER

int[] randomNumbers = RandomizeArray(numbers);
//shuffles the array

I generated a set of numbers in order, and then shuffled them, which works fine.

But, my method of grouping people has been based off ifelse conditions which don't work so well. So is there an efficient method to put this ArrayList of names into user-desired groups?

Upvotes: 0

Views: 59

Answers (1)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51445

I have x number of students.

I have n number of groups.

I'm assuming there's no other criteria for dividing students into groups.

Basically, you divide the number of groups into the number of students. Yes, you have to deal with a remainder. But it's straightforward.

Here are the results of a test. I created 53 students and shuffled them. I then divided the students into 4 groups. I manually formatted the output to fit in the answer. Group 1 has 14 students, while the remaining groups have 13 students.

Students: 42, 27, 5, 26, 32, 30, 44, 10, 17, 29, 40, 52, 
    47, 38, 49, 18, 46, 24, 34, 12, 13, 53, 35, 20, 1, 
    2, 41, 23, 43, 28, 8, 11, 50, 37, 9, 7, 48, 3, 33, 
    25, 31, 15, 22, 21, 14, 45, 36, 16, 51, 19, 4, 6, 39
Group 1: 42, 27, 5, 26, 32, 30, 44, 10, 17, 29, 40, 52, 47, 38
Group 2: 49, 18, 46, 24, 34, 12, 13, 53, 35, 20, 1, 2, 41
Group 3: 23, 43, 28, 8, 11, 50, 37, 9, 7, 48, 3, 33, 25
Group 4: 31, 15, 22, 21, 14, 45, 36, 16, 51, 19, 4, 6, 39

And here's the code. The groupStudents and sortStudents methods are the methods that do the work.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class StudentGroups {

    public static void main(String[] args) {
        int numberOfGroups = 4;
        int numberOfStudents = 53;
        StudentGroups sg = new StudentGroups(numberOfStudents);
        sg.printStudents();
        List<List<Integer>> groups = sg.groupStudents(
                numberOfGroups);
        sg.printGroups(groups);
    }

    private List<Integer> students;

    public StudentGroups(int numberOfStudents) {
        students = new ArrayList<>(numberOfStudents);
        for (int i = 0; i < numberOfStudents; i++) {
            students.add(i + 1);
        }
        Collections.shuffle(students);
    }

    public void printStudents() {
        System.out.print("Students: ");
        for (int i = 0; i < students.size(); i++) {
            System.out.print(students.get(i));
            if (i < (students.size() - 1)) {
                System.out.print(", ");
            }
        }
        System.out.println();
    }

    public List<List<Integer>> groupStudents(int groups) {
        List<List<Integer>> output = new ArrayList<>();

        int size = students.size();
        int group = size / groups;
        int remainder = size % groups;
        sortStudents(output, size, group, remainder);

        return output;
    }

    private void sortStudents(List<List<Integer>> output, 
            int size, int group, int remainder) {
        List<Integer> list = new ArrayList<>();
        int count = 0;

        for (int i = 0; i < size; i++) {            
            list.add(students.get(i));

            if (count == 0 && remainder > 0) {
                if (group > 0) {
                    list.add(students.get(++i));
                }
                remainder--;
            }

            if (++count >= group) {
                addList(output, list);
                list = new ArrayList<>();
                count = 0;
            }
        }

        addList(output, list);
    }

    private void addList(List<List<Integer>> output, 
            List<Integer> list) {
        if (list.size() > 0) {
            output.add(list);
        }
    }

    public void printGroups(List<List<Integer>> groups) {
        for (int j = 0; j < groups.size(); j++) {
            System.out.print("Group ");
            System.out.print(j + 1);
            System.out.print(": ");
            List<Integer> students = groups.get(j);
            for (int i = 0; i < students.size(); i++) {
                System.out.print(students.get(i));
                if (i < (students.size() - 1)) {
                    System.out.print(", ");
                }
            }
            System.out.println();
        }
    }

}

Upvotes: 1

Related Questions