bob9123
bob9123

Reputation: 745

The use of HashMaps with reading/writing txt files- Minor fix

Code:

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();
    }

}

So what this code does is so far is read from a text file, store/format it and write onto a new text file.

The output textfile looks something like this:

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

Now this is right way apart from the Marks. Each student has two different marks which are stored on different text files. I want to import mark info from both text files and output to one single text file.

The code only gets the marks from the second (IR102) file and not (IR101) file. I'm assuming because the second code overwrites

Do I need to make another new hash?

PS: With this done, is it possible to get the average of the two marks with this method?

---Additional Code:

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

Plus more to this

Upvotes: 1

Views: 374

Answers (1)

merlin2011
merlin2011

Reputation: 75555

First of all, you are re-opening and writing to File_2.txt twice, and the second open is blowing away the first one. Instead, you want to store your data when you read the second and third file and then output at the end.

One clean way to handle this storage is to create a new class called Student which will consist of an ID, a name, and a list of marks.

Note that if you want to compute an average, it probably makes most sense to represent the marks using a numeric type like 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();
    }
}

Upvotes: 1

Related Questions