Search code examples
javaserializationiofilewriter

Reading and Analysing data from text file - Big issue


This is what I have so far:

public class test1 {

public static void main(String args[]) throws IOException {
    BufferedReader br = null;
    BufferedWriter bw = null;


        br = new BufferedReader(new FileReader("IRStudents.txt"));
        bw = new BufferedWriter(new FileWriter("File_2.txt"));
        String line = br.readLine();



    for (int i = 1; i <= 10 && line != null; i++) {
        bw.write(line);
        bw.newLine();
        bw.write("- - - - - - - - - - -");
        bw.newLine();

        line = br.readLine();
    }


        System.out.println("Lines are Successfully copied!");

        br.close();

        bw.close();
    }

}

So right now text gets written into a new text document. The format is like this:(User ID Name)

25987 Alan
- - - - - -
25954 Betty
- - - - - -

etc

Now I got another file with UserID and Marks with a layout like this:

25220 68.7
25987 51.5
25954 22.2 

Now I want to make a way for Java to analyse the UserID and merge the marks with the specific name and marks that it's associated to. For example this is what it should look like at the end.

25987 Alan
Marks: 51.5
- - - - - - 
25954 Betty
Marks: 22.2
- - - - - - 

Now I know this is alot to ask and do not expect a full solution of code but i'm a very new java programmer and will appreciate advice and what direction i should take.

Thank You.


Solution

  • The way to do this (with small datasets) is to store the data of one file and merge the data whilst reading the other file like in my code below (you did get a full solution after all :) ) Oh and please note that if you are going to work with huge datasets that you would probably want to use a database instead. And my code will not list users that do not have a mark (though you can fix that quite easily)

    The code: (this code asumes that there is a file called IRStudents.txt and a file called Marks.txt)

    public static void main(String[] args) throws IOException{
        //declare reader and writer
        BufferedReader reader = null;
        PrintWriter writer = null;
    
        //hash map to store the data of the first file
        Map<String, String> names = 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("Marks.txt"))));
        while((line = reader.readLine()) != null){
            arg = line.split(" ");
            writer.println(arg[0] + " " + names.get(arg[0]));
            writer.println("Marks: " + arg[1]);
            writer.println("- - - - - -");
        }
        writer.flush();
        writer.close();
        reader.close();
    }
    

    Hope this helps :)