javasortinggroupingpreferencesdiscriminator

generating groups from txt file and generating them based on preferences


enter image description hereI am designing a group generator that takes in preferences such as “mix gender”, “mix nationality”... I am putting a list of student names, followed by nationality and gene set, in an arraylist. What is the easiest way to generate groups, based on user input, that each group consists of people from different nationalities, or balanced gender.

 public ArrayList<String> readEachWord(String className)
    {
        ArrayList<String> readword = new ArrayList<String>();
        Scanner sc2 = null;
        try {
        sc2 = new Scanner(new File(className + ".txt"));
    } catch (FileNotFoundException e) {

        System.out.println("error, didnt find file");
        e.printStackTrace();  
    }
    while (sc2.hasNextLine()) {
            Scanner s2 = new Scanner(sc2.nextLine());
        while (s2.hasNext()) {
            String s = s2.next();
            readword.add(s);
        }
    }
    return readword;


    }

I am using this to read a text file, and on each line, I have each student's name nationality and gender. I put them into an ArrayList and am right now trying to figure out how to evenly distribute them based on the user-desired group numbers.

I am using a txt file to store all the information since this group generator is customized for my school.


Solution

  • You can use the groupinBy method

    basic tutorial

    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
    
    class Scratch {
        public static void main(String[] args) {
    
            String student1 = "Macie American Female";
            String student2 = "Yago Brazilian Male";
            String student3 = "Tom American Male";
    
            List<String> students = Arrays.asList(student1, student2, student3);
    
            System.out.println(groupByGender(students));
            System.out.println(groupByNationality(students));
        }
    
        private static Map<String, List<Student>> groupByNationality(List<String> students) {
            return students.stream().map(s -> mapToStudent(s)).collect(Collectors.groupingBy(Student::getNationality));
        }
    
        private static Map<String, List<Student>> groupByGender(List<String> students) {
            return students.stream().map(s -> mapToStudent(s)).collect(Collectors.groupingBy(Student::getGender));
        }
    
        private static Student mapToStudent(String s) {
            String[] ss = s.split(" ");
            Student student = new Student();
            student.setName(ss[0]);
            student.setNationality(ss[1]);
            student.setGender(ss[2]);
            return student;
        }
    
        private static class Student {
            String name;
            String nationality;
            String gender;
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public String getNationality() {
                return nationality;
            }
    
            public void setNationality(String nationality) {
                this.nationality = nationality;
            }
    
            public String getGender() {
                return gender;
            }
    
            public void setGender(String gender) {
                this.gender = gender;
            }
    
            @Override
            public String toString() {
                return "Student{" +
                        "name='" + name + '\'' +
                        ", nationality='" + nationality + '\'' +
                        ", gender='" + gender + '\'' +
                        '}';
            }
        }
    
    
    }