HashMaps与读/写文本文件的使用 - 次要修复

时间:2015-03-26 01:21:24

标签: java file hash

代码:

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)
 - - - - - -  - - -  - - - - - - - - - - - - - - - - 

此外还有更多

1 个答案:

答案 0 :(得分:1)

首先,你重新开放并写入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();
    }
}