Bonsoir
Je veux faire un programme qui à partir d'un fichier lit pour chaque étudiant (identifié par un numéro studentID) les différents score obtenus,
donc on un fichier qui contient par exemple:
Le programme doit identifier le numéro de l'étudiant qui est le premier numéro de la ligne, j'utilise un split et collecter la liste des score obtenus dans pour calculer la somme ou une moyenne... ( le sore c'est le 4ème champ , le dernier numéro)1 Q0 DerOpthalmologe/80950033 0 5.960979448916666
1 Q0 DerRadiologe/80380710 1 5.836199971080442
1 Q0 MonatsschriftKinderheilkunde/01480013 2 5.836199971080442
2 Q0 DerUnfallchirurg/81010830 2 7.503564829783422
2 Q0 DerChirurg/80690854 3 7.503564829783422
2 Q0 DerChirurg/60671226 4 6.176200952599098
....
j'ai trouvé un code que j'ai voulou adpater qui utilise différente classe, mais ca ne marche pas.
1 ère classe loader qui lit le fichier:
2ème classe student:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 /* package query; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; public class Loader { /** * * @author */ public static ArrayList<Student> loadStudentsData() throws FileNotFoundException, UnsupportedEncodingException, IOException { String fichier ="C:/normalisation/file.res"; String fichier2 ="C:/normalisation/res"; ArrayList<Student> studentsCollection = new ArrayList<Student>(100); BufferedReader br = new BufferedReader(new InputStreamReader(new java.io.FileInputStream(fichier), "UTF-8")); String ligne; String recordLine = new String(); String lineItems[] = new String[3]; Student Element = new Student(); StudentTest studenTest = new StudentTest(); while ((ligne=br.readLine())!=null){ String test = ligne; System.out.println(test); lineItems = ligne.split(" ") ; System.out.println(lineItems[0]+"\n"); System.out.println( lineItems[0] + " " + lineItems[1] + " " + lineItems[2] ); Element.setStudentID( Integer.parseInt(lineItems[0]) ); // second item is ID int index ; if ( (index = studentsCollection.indexOf(Element) ) > -1 ) { Element = studentsCollection.get(index); System.out.println("test : "+studentsCollection.indexOf(Element)+"\n"); } studenTest.setTestDate(Integer.parseInt(lineItems[0])); studenTest.setTestScore( Float.parseFloat(lineItems[4] ) ); Element.addStudentTest(studenTest); if ( index == -1) { studentsCollection.add(Element); } } return studentsCollection; } }
3ème classe qui contient aussi les scores:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
57
58
59
60 /* */ package query; import java.util.ArrayList; /** * * @author */ public class Student { /* * This is the main student class to hold each record entry * */ private int studentID; private ArrayList<StudentTest> testsCollections ; public Student() { testsCollections = new ArrayList<StudentTest>(); } public void setStudentID(int id) { this.studentID = id; } public int getStudentID() { return this.studentID ; } public void addStudentTest(StudentTest test) { this.testsCollections.add(test); } public ArrayList<StudentTest> getStudentTestsCollection() { return this.testsCollections ; } @Override public boolean equals(Object v) { return this.studentID == ((Student)v).studentID ; } @Override public int hashCode() { int hash = 5; hash = 89 + hash + (this.testsCollections != null ? this.testsCollections.hashCode() : 0); hash += 89 + hash + this.studentID; return hash; } }
classe searchresult qui fait le calcul:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package query; public class StudentTest { /* * This student Test class hold record details by id and score * */ private int idi; private double score; public void setTestId(int idi) { this.date = date; } public int getTestId() { return this.idi ; } public void setTestScore(double score) { this.score = score; } public double getTestScore() { return this.score ; } }
la classe qui cherche les scores selon l'identifiant:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package query; import java.util.TreeSet; /* * This class will hold all results score and will return final score * */ public class SearchResult implements Comparable{ private int student; private TreeSet<Double> scores = new TreeSet<Double>(); public void setStudentID(int student) { this.student = student; } public int getStudentID() { return this.student ; } public double getFinalScore() { Object[] arr = scores.toArray(); double finalScore = 0; int index = 0; for(int i=arr.length - 1; i >= 0; i--) { finalScore += (double)arr[i]; System.out.println( "student id "+finalScore ); index++; if( index == 5 ) break; } return finalScore / index;// } public void addScore(double dl) { scores.add(dl); } @Override public String toString() { return this.student + " - " + getFinalScore(); } @Override public boolean equals(Object v) { return this.student == ((SearchResult)v).student ; } @Override public int hashCode() { int hash = 5; hash = 89 + hash + (this.scores != null ? this.scores.hashCode() : 0); hash += 89 + hash + this.student; return hash; } @Override public int compareTo(Object o) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }
classe qui contient le main et qui cherche le score selon l'dientifant de l'étudiant:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package query; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.TreeSet; /* * This class works as a controller to search records by date and populate Search Results collection * */ public class DataSearchController { public static TreeSet<SearchResult> searchStudentScore(int studentID) throws IOException { ArrayList<Student> students = Loader.loadStudentsData(); TreeSet<SearchResult> results =new TreeSet<SearchResult>() ; for(Iterator<Student> iterator = students.iterator() ; iterator.hasNext(); ) { Student st = iterator.next(); System.out.println( "student ili"+st.getStudentID()); SearchResult rs =new SearchResult(); for( Iterator<StudentTest> testItrator = st.getStudentTestsCollection().iterator(); testItrator.hasNext(); ) { StudentTest stest = testItrator.next(); rs.setStudentID(st.getStudentID()); System.out.println( "studenttest "+stest.getTestScore()); rs.addScore( stest.getTestScore() ); } results.add(rs); } return results; } }
ce que j'arrive pas à comprendre pourquoi ;
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* package query; import java.io.IOException; import java.util.Iterator; import java.util.TreeSet; /* * Here is the main Class with main method , it is just going to search all loaded recorded and show final results * */ public class Command { public static void main(String[] arg) throws IOException { TreeSet<SearchResult> sr = DataSearchController.searchStudentScore(2); for(Iterator<SearchResult> it = sr.iterator(); it.hasNext();) { SearchResult st = it.next(); System.out.println( "opa "+st.getStudentID() ); } } }ne retourne rien
Code : Sélectionner tout - Visualiser dans une fenêtre à part return studentsCollection;
et quel est son utilité puisque qu'il y a aucune instruction dans la classe loader qui remplit cette structure
Partager