gpt4 book ai didi

java - 使用 HashMap 读取/写入 txt 文件 - 小修复

转载 作者:行者123 更新时间:2023-11-30 08:18:50 25 4
gpt4 key购买 nike

代码:

public class test1 {


public static void main(String[] args) throws IOException {
//declare reader and writer
BufferedReader reader = null;
PrintWriter writer = null;

//hash maps to store the data
HashMap<String, String> names = new HashMap<String, String>();
HashMap<String, String> names1 = new HashMap<String, String>();


//read the first file and store the data
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IRStudents.txt"))));
String line;
String[] arg;
while ((line = reader.readLine()) != null) {
if (!line.startsWith("-")) {
arg = line.split(" ");


names.put(arg[0], arg[1]);


}
}
reader.close();

//read the second file, merge the data and output the data to the out file
writer = new PrintWriter(new FileOutputStream(new File("File_2.txt")));
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IR101.txt"))));
while((line = reader.readLine()) != null){
arg = line.split(" ");
writer.println(arg[0] + " " + names.get(arg[0]));
writer.println("Marks: " + arg[1] + " Marks2:");
writer.println("- - - - - -"); }

//read the third, file, merge the data and output the data to the out file
writer = new PrintWriter(new FileOutputStream(new File("File_2.txt")));
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IR102.txt"))));
while((line = reader.readLine()) != null){
arg = line.split(" ");
writer.println(arg[0] + " " + names.get(arg[0]));
writer.println("Marks: " + " Marks2:" + arg[1]);
writer.println("- - - - - -"); }


writer.flush();
writer.close();
reader.close();
}

}

到目前为止,这段代码的作用是从文本文件中读取、存储/格式化它并写入新的文本文件。

输出文本文件看起来像这样:

25987 Alan
Marks: Marks2:20.7
- - - - - -
25954 Betty
Marks: Marks2:63.4
- - - - - -
25218 Helen
Marks: Marks2:53.4
- - - - - -

现在这与标记相距甚远。每个学生都有两个不同的分数,存储在不同的文本文件中。我想从两个文本文件导入标记信息并输出到一个文本文件。

代码仅从第二个 (IR102) 文件获取标记,而不是 (IR101) 文件。我假设是因为第二个代码覆盖了

我需要再创建一个新的哈希值吗?

PS:这样的话,用这个方法是否可以得到两个分数的平均值呢?

---附加代码:

 //So this is the IRStudents file. This contains Student ID and Name.
25987 Alan
25954 Betty
25654 Chris
25622 David
//So the StudentID has to match all the student Id with the mark files.

//File 1 Mark 1 with marks in it.
25987 35.6
25954 70.2
25654 58.6
25622 65.0

//File 2 Mark 2 with marks in it.
25987 20.7
25954 63.4
25654 35.1
25622 57.8

//So the output file should be.

25987 Alan
Marks1: 35.6 Mark2: 20.7 AverageMark: (35.6+20.7)/2
- - - - - - - - - - - - - - - - - - - - - - - - -
25954 Betty
Marks1: 70.2 Mark2: 63.4 AverageMark:(average)
- - - - - - - - - - - - - - - - - - - - - - - - -

还有更多内容

最佳答案

首先,您重新打开并写入 File_2.txt 两次,第二次打开会清除第一次打开的结果。相反,您希望在读取第二个和第三个文件时存储数据,然后在最后输出。

处理此存储的一种简洁方法是创建一个名为 Student 的新类,该类将包含 ID、名称和标记列表。

请注意,如果您想计算平均值,使用 double 等数字类型表示标记可能最有意义。

import java.util.*;
import java.io.*;

public class test1 {
static class Student {
String id;
String name;
List<Double> marks;

public Student(String id, String name) {
this.id = id;
this.name = name;
marks = new ArrayList<Double>();
}
public void addMark(Double d) {
marks.add(d);
}
public void writeToPW(PrintWriter out) {
out.println(id + " " + name);
double d = 0;
for (int i = 0; i < marks.size(); i++) {
out.printf("Marks%d: %.2f ", (i+1), marks.get(i));
d += marks.get(i);
}
out.println("AverageMark: " + d / marks.size());
out.println("- - - - - -");
}
}
public static void main(String[] args) throws IOException {
//declare reader and writer
BufferedReader reader = null;
PrintWriter writer = null;

//hash maps to store the data
HashMap<String, Student> students = new HashMap<String, Student>();

// list to maintain the original order
List<Student> orderedStudents = new ArrayList<Student>();


//read the first file and store the data
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IRStudents.txt"))));
String line;
String[] arg;
while ((line = reader.readLine()) != null) {
if (!line.startsWith("-")) {
arg = line.split(" ");
Student student = new Student(arg[0], arg[1]);
students.put(arg[0], student);
orderedStudents.add(student);
}
}
reader.close();


reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IR101.txt"))));
while((line = reader.readLine()) != null){
arg = line.split(" ");
students.get(arg[0]).addMark(Double.parseDouble(arg[1]));
}


reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IR102.txt"))));
while((line = reader.readLine()) != null){
arg = line.split(" ");
students.get(arg[0]).addMark(Double.parseDouble(arg[1]));
}

// Now we can do writing.
writer = new PrintWriter(new FileOutputStream(new File("File_2.txt")));
for (Student s: orderedStudents) {
s.writeToPW(writer);
}
writer.close();
}
}

关于java - 使用 HashMap 读取/写入 txt 文件 - 小修复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29269487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com