从 txt 文件生成组并根据首选项生成它们

generating groups from txt file and generating them based on preferences

我正在设计一个群组生成器,它接受诸如“混合性别”、“混合国籍”等偏好...我正在放置一个学生姓名列表,然后是国籍和基因集,在数组列表中。根据用户输入生成组的最简单方法是什么,每个组由来自不同国籍或平衡性别的人组成。

 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;


    }

我正在用它来读取一个文本文件,在每一行中,我都有每个学生的姓名、国籍和性别。我将它们放入 ArrayList 中,现在正试图弄清楚如何根据用户所需的组号均匀分布它们。

我正在使用一个 txt 文件来存储所有信息,因为这个组生成器是为我的学校定制的。

您可以使用groupinBy方法

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 + '\'' +
                    '}';
        }
    }


}

首先,如果将 while 循环放在 try 块中会更好,因为如果找不到文件,您不想到达那里。

其次,您不需要创建一个新的 Scanner 实例来读取每一行。您可以简单地逐字阅读您的文件:

while (sc2.hasNext())
    readword.add(sc2.next());

要根据学生的国籍对学生进行分组,您可以这样做:

String nationality = [UserInput] ;
List<String> group = new ArrayList<>();
for (int i = 0; i < readword.size(); i++)
    if (readword.get(i + 1).equals(nationality)
        group.add(readword.get(i));