从.txt文件中获取的学生列表中计算平均值 [英] Calculating Average from a list of students taken from a .txt file

查看:238
本文介绍了从.txt文件中获取的学生列表中计算平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单独的.txt文件,其中有一个学生"列表,该列表的侧面带有从0到10的自己的标记,这是一个有关.txt外观的示例:

I have a separated .txt file in which there is a list of "students" with their own mark on the side that goes from 0 to 10, here is an example on how the .txt looks like:

马克2
艾伦3
路加福音7
埃伦9
Jhon 5
马克4
埃伦10
路加福音1
俊1
俊7
埃伦5
马克3
标记7

Mark 2
Elen 3
Luke 7
Elen 9
Jhon 5
Mark 4
Elen 10
Luke 1
Jhon 1
Jhon 7
Elen 5
Mark 3
Mark 7

我想做的是计算每个学生的平均值(用double表示),以便输出看起来像这样:

What I want to do is calculating the Average of each student (expressed in double) so that the output would look like this:

Mark: 4.0
Elen: 6.75
Luke: 4.0
Jhon: 4.33

这是我想出的,现在我只设法使用Properties列出了学生姓名而没有重复,但是每个人的侧面显示的数字显然是最后一个程序找到了.
我在实现GUI时将其包含在按钮actionlistener中,通过按按钮,上面显示的输出是appendTextArea中:

Here is what I've come up with, for now I've only managed to use Properties to list the student names without repetition, but the number shown on the side of each one of them is obviously the last one the program finds.
I've included it on a button actionlistener as I'm implementing a GUI, by pressing the button the output shown above is append in a TextArea:

 b1.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent d) {

   try {
      File file = new File("RegistroVoti.txt");
      FileInputStream fileInput = new FileInputStream(file);
      Properties properties = new Properties();
      properties.load(fileInput);
      fileInput.close();

      Enumeration enuKeys = properties.keys();
      while (enuKeys.hasMoreElements()) {
        String key = (String) enuKeys.nextElement();
        String value = properties.getProperty(key);
        l1.append(key + ": " + value + "\n");
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
});

我当时想使用Collectors来计算平均值,但实际上我不知道如何实现...

I was thinking to use Collectors to calculate the average, but I actually don't know how to implement it...

感谢您的帮助!

提前谢谢!

推荐答案

我喜欢做这种事情的方法是使用Map s和List s.

The way I like to do this kind of thing is using Maps and Lists.

要读取文件中的行,我喜欢 nio 的阅读方式,所以我会做

To read the lines from the file, I'm fond of the nio way of reading, so I would do

List<String> lines = Files.readAllLines(Paths.get("RegistroVoti.txt"));

然后,您可以创建 HashMap <String, List <Integer>>,它将存储每个人的姓名和与他们相关的电话号码列表:

Then, you can make a HashMap<String,List<Integer>>, which will store each person's name and a list of the numbers associated with them:

HashMap<String, List<Integer>> studentMarks = new HashMap<>();

然后,对每个循环使用a,遍历每一行并将每个数字添加到哈希图中:

Then, using a for each loop, go through each of the lines and add each number to the hash map:

for (String line : lines) {
    String[] parts = line.split(" ");
    if (studentMarks.get(parts[0]) == null) {
        studentMarks.put(parts[0], new ArrayList<>());
    }
    studentMarks.get(parts[0]).add(Integer.parseInt(parts[1]));
}

然后,您可以遍历地图中的每个条目并计算关联列表的平均值:

Then, you can go through each entry in the map and calculate the average of the associated list:

for (String name : studentMarks.keySet()) {
    System.out.println(name + " " + studentMarks.get(name).stream().mapToInt(i -> i).average().getAsDouble());
}

(请注意,这是Java 8

(Note that this is a Java 8 stream solution; you could just as easily write a for loop to calculate it in earlier versions)

有关我已使用的某些东西的更多信息,请参见:

For more information on some of the things I've used, see:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆