Reputation: 745
Code so far:
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>();
//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();
}
}
So the output in the text file looks like:
25220 Fiona
Marks: 68.3
- - - - - -
25212 Greg
Marks: 70.5
- - - - - -
I have ANOTHER text file with another set of marks with the same layout as the first mark file.
Now I want to add a new set of marks to the set of data So it should look like this:
25220 Fiona
Marks: 68.3 Marks2: 21.2
- - - - - -
25212 Greg
Marks: 70.5 Marks2: 23.43
- - - - - -
So what can I do to add? I assume I have to add a new Hashmap for the new text document? But when I tried doing all of that it never fully works.
IR Student:
25987 Alan
25954 Betty
25654 Chris
25622 David
Upvotes: 0
Views: 1059
Reputation: 661
You could do the following too.
package toBeDeleted;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class MarksProcessor {
private final Map<String, Record> map = new HashMap<>();
public static void main(String[] args) {
String fileName = "file1.txt"; // change it to your specific file.
MarksProcessor marksProcessor = new MarksProcessor();
marksProcessor.processFile(fileName, 0);
fileName = "file2.txt";
marksProcessor.processFile(fileName, 1);
marksProcessor.writeData();
}
private void processFile(String fileName, int marksIndex) {
try(/*specify your reader resources here*/) {
// read the first record and get rollNumber, name and marks.
String roll = "valueYouGot";
double value = 0.0; // the value you read.
Record record = map.get(roll);
// if record is null, you need to create one
// and put it into the map.
//record.updateMarks(marksndex, value);
}
}
private void writeData() {
// if this needs to be written to a file/stream, create a writer.
for (Map.Entry<String, Record> entry : map.entrySet()) {
String roll = entry.getKey();
Record record = entry.getValue();
if (record != null) {
String name = record.getName();
double marks1 = record.getMarks(0);
double marks2 = record.getMarks(1);
// Now you have all the values. Print them
// however you like. Wherever you like.
}
}
}
static class Record {
private String name;
private double[] marks = new double[2];
Record(String name) {
this.name = name;
}
public String getName() {
return name;
}
public double getMarks(int index) {
if (index < 0 || index > 1)
throw new IllegalArgumentException("index should be 0 or 1 but"
+ " the supplied index was " + index);
return marks[index];
}
public void updateMarks(int index, double value ) {
if (index < 0 || index > 1)
throw new IllegalArgumentException("index should be 0 or 1 but"
+ " the supplied index was " + index);
marks[index] = value;
}
@Override
public String toString() {
return "the way you want to type your output";
}
}
}
Upvotes: 1
Reputation: 661
I think you are doing too much String manipulation. And if you will have more marks' files to process in a similar way, the String manipulation is likely going to increase which could make your code less readable and could give more room for errors. I think following would be a better approach.
You could create a MarksRecord class with the following structure.
public class MarksRecord {
private String subject; // or whatever this variable name should be.
// in your case it should hold value marks1.
private double marks;
}
Similarly you could create an immutable Student/similar class as follows. This could be a value class with equals and hashCode methods based on the first number you are reading in each file. I am guessing it is roll number or similar that can identify a student in a unique way.
public final class Student {
private final String rollNumber;
private final String name;
// equals, hashCode, and other methods.
}
Then in your main method you could have a
Map<Student, ArrayList<MarksRecord>>
. Alternatively you could also use a
Map<String, ArrayList<MarksRecord>>
where the first String is the roll number of a Student record.
This way every time you have a new file of marks, your data structure will be able to accomodate it.
Upvotes: 1
Reputation: 1008
I think I understand your problem, let me know if this is incorrect.
The requirement is to have all marks that the person receved on its own line...
Theres two print functions in the System.out stream.
print and println
arg = line.split(" ");
writer.println(arg[0] + " " + names.get(arg[0]));
writer.print("Marks: " + arg[1]);
for(int i = 2; i < args.length; i++){
writer.println(" Marks" + i + ": " + arg[i]);
}
writer.println("\n- - - - - -");
Upvotes: 0
Reputation: 657
When adding the new marks, use this to add them to the already existing ones:
String key = arg[0];
String secondMarks = arg[1];
String theMarks = names.get(key);
theMarks = theMarks + " Marks2: " + secondMarks;
names.put(key, theMarks);
Upvotes: 0