私が書いているプログラムは、3 つの異なるテキスト ファイルを読み取ります。1 つのテキスト ファイルには名前が含まれ、他の 2 つのテキスト ファイルにはマークが含まれています。
これですべてを正しく実行できましたが、追加したいことが 1 つありますが、うまくいきません。
したがって、現在、出力ファイルは次のようになります。
25987 Alan
IR101: 35.6 IR102: 20.7 Aggregate: 28.2
Class: Fail Outcome: Repeat Year!
-------------------------------------------------------
25954 Betty
IR101: 70.2 IR102: 63.4 Aggregate: 66.8
Class: 2.1 Outcome: Proceed to Stage 2!
-------------------------------------------------------
25654 Chris
IR101: 58.6 IR102: 35.1 Aggregate: 46.9
Class: Fail Outcome: Resit IR102!
-------------------------------------------------------
Etc
したがって、私のプログラムは、名前付きのテキスト ファイルから名前の観点から順序を出力します。たとえば、すべての名前が順番に含まれているテキスト ファイルの 1 つは、次のようになります。 Alan /n Betty /n Chris
ここで、順序をテキスト ファイルの名前にする必要はありません。順序を集計マークの降順にしたいのです。したがって、順序は次のようになります。
25954 Betty
IR101: 70.2 IR102: 63.4 Aggregate: 66.8
Class: 2.1 Outcome: Proceed to Stage 2!
-------------------------------------------------------
25654 Chris
IR101: 58.6 IR102: 35.1 Aggregate: 46.9
Class: Fail Outcome: Resit IR102!
-------------------------------------------------------
25987 Alan
IR101: 35.6 IR102: 20.7 Aggregate: 28.2
Class: Fail Outcome: Repeat Year!
-------------------------------------------------------
長い間さまざまなソリューションを試してきましたが、すべて失敗しています。
プログラムのコード:
public class SORTING {
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("IR10%d: %.1f ", (i+1), marks.get(i));
d += marks.get(i);
}
out.printf("Aggregate: %.1f ", + d / marks.size());
out.println("\n");
double aggregate = (d/marks.size());
if ((marks.get(0)<40)&&(marks.get(1)>=40)){
out.print("Class: Fail" + " Outcome: Resit IR101");
}
if ((marks.get(0)>=40)&&(marks.get(1)<40)){
out.print("Class: Fail" + " Outcome: Resit IR102!");
}
if ((marks.get(0)<40)&&(marks.get(1)<40)){
out.print("Class: Fail" + " Outcome: Repeat Year!");
}
if((marks.get(0)>40)&&(marks.get(1)>40)&&(aggregate>70)){
out.print("Class: 1st" + " Outcome: Proceed to Stage 2!");
}
if((marks.get(0)>40)&&(marks.get(1)>40)&&(aggregate>=60)&&(aggregate<=69.9)){
out.print("Class: 2.1" + " Outcome: Proceed to Stage 2!");
}
//2.2 Class degree code.
if((marks.get(0)>40)&&(marks.get(1)>40)&&(aggregate>=50)&&(aggregate<=59.9)){
out.print("Class: 2.2" + " Outcome: Proceed to Stage 2!");
}
if((marks.get(0)>40)&&(marks.get(1)>40)&&(aggregate>=40)&&(aggregate<=49.9)){
out.print("Class: 3rd" + " Outcome: Proceed to Stage 2!");
}
out.println("\n");
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();
//read the second file, merge the data and output the data to the out file
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("RankedList.txt")));
for (Student s: orderedStudents) {
s.writeToPW(writer);
}
writer.close();
}
}