-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadStudentFinalMark.java
More file actions
56 lines (42 loc) · 1.71 KB
/
ReadStudentFinalMark.java
File metadata and controls
56 lines (42 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
public class ReadStudentFinalMark {
public static void main(String[] args) {
ArrayList<Student> studentList = new ArrayList<>();
String filename = "student_data.txt";
try {
studentList = readStudent(filename);
}catch (FileNotFoundException e){
System.out.println(e.getMessage());
System.exit(1);
}
Collections.sort(studentList,new Sort());// OR Collections.sort(studentList,new SortDes()); for descending
//Collections.
for (Student stuwe: studentList) {
//System.out.print(stuwe);
String s = String.format("%-10d %-15s %s: %.1f",stuwe.getStudentID(),stuwe.getsName(),stuwe.getfName(),stuwe.getFinalMark());
System.out.println(s);
}
}
public static ArrayList<Student> readStudent(String filename)throws FileNotFoundException{
ArrayList<Student> studentList = new ArrayList<>();
Scanner file = new Scanner(new File(filename));
while(file.hasNextLine()){
Scanner in = new Scanner(file.nextLine());
Student s = new Student();
s.setStudentID(in.nextInt());
s.setfName(in.next());
s.setsName(in.next());
s.setCourseWork1(in.nextInt());
s.setCourseWork2(in.nextInt());
s.setExamMark(in.nextInt());
studentList.add(s);
in.close();// have a look at how to deal with closing
}
file.close();
return studentList;
}
}