所以我正在执行这个程序,我浏览指定文件夹中的每个文件夹,如果thoes文件夹中有任何文件,我会阅读它们。
文件如下所示:
file name: grades
Mait 5 5 5
Kati 3 3 3
Vello 4 4 4
所以我读了每一行,每一行中的第0个元素是我添加到数组中的学生的名字。之后,我取得分数,然后计算它们的中位数。
此外,文件位于由类名命名的文件夹中。例如,数学中包含每个数学成绩。
现在程序的工作是阅读所有文件,然后生成一个输出文件,其中包含每个班级中的每个学生姓名和中位数,之后是所有成绩的中位数。
这就是我的输出现在的样子:
Nimi: Mait Keskmine hinne: 5.0 Aine: ..\text\students\english\grades
Nimi: Kati Keskmine hinne: 3.0 Aine: ..\text\students\english\grades
Nimi: Vello Keskmine hinne: 4.0 Aine: ..\text\students\english\grades
Nimi: Mait Keskmine hinne: 1.0 Aine: ..\text\students\math\grades
Nimi: Kati Keskmine hinne: 2.0 Aine: ..\text\students\math\grades
它不是英文,而是名称形式:中位数; ,班级名称;
现在的问题是,当我将相同的东西打印到控制台时,而不是文件我得到了这个:
Nimi: Mait Keskmine hinne: 5.0 Aine: ..\text\students\english\grades
Nimi: Kati Keskmine hinne: 3.0 Aine: ..\text\students\english\grades
Nimi: Vello Keskmine hinne: 4.0 Aine: ..\text\students\english\grades
Nimi: Mait Keskmine hinne: 5.0 Aine: ..\text\students\english\grades
Nimi: Kati Keskmine hinne: 3.0 Aine: ..\text\students\english\grades
Nimi: Vello Keskmine hinne: 4.0 Aine: ..\text\students\english\grades
Nimi: Mait Keskmine hinne: 1.0 Aine: ..\text\students\math\grades
Nimi: Kati Keskmine hinne: 2.0 Aine: ..\text\students\math\grades
现在你可以看到,第一个被读取的文件是重复的,或者出于某种原因。当我只有一个程序可以从中读取的文件夹时,不会发生此问题。但它适用于更多文件夹。
现在为什么这对我来说是一个问题?因为我无法看到如何计算一个学生在学校所有成绩的中位数,当我在我的课程中有重复的值时。
这里可能存在什么问题,如何解决?
这是我在项目中的所有课程,所以你们可以看到代码:
Main.java:
package files;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
Path lessons = Paths.get("../text/students");
FileVisitor file = new FileVisitor();
try {
Files.walkFileTree(lessons, file);
} catch (IOException e) {
e.printStackTrace();
}
List<Path> files = file.askFiles();
List<Student> students = new ArrayList<>();
for(Path adress:files){
//System.out.println(adress.getFileName());
try {
BufferedReader textIn = new BufferedReader(new FileReader(adress.toString()));
String line;
while((line = textIn.readLine()) != null){
String[] readContent = line.split(" ");
Student student = new Student(readContent[0],adress.toString());
//System.out.println("sad");
for(int i = 1;i < readContent.length; i++){
student.addGrade(readContent[i]);
//System.out.println(adress.getParent()+ " ");
}
students.add(student);
}
PrintWriter output = new PrintWriter(new FileWriter("../text/opilased_out"));
for(Student i:students){
output.println("Nimi: "+i.askName() + " " + "Keskmine hinne: "+i.askMedian() + " " + "Aine: "+i.askSubject());
System.out.println("Nimi: "+i.askName() + " " + "Keskmine hinne: "+i.askMedian() + " " + "Aine: "+i.askSubject());
}
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Student.java:
package files;
import java.util.ArrayList;
import java.util.List;
public class Student {
String studentName;
List<Integer> studentGrades = new ArrayList<>();
String subjectName;
double sum;
public Student(String name, String subject) {
subjectName = subject;
studentName = name;
}
public String askSubject(){
return subjectName;
}
public String askName(){
return studentName;
}
public void addGrade(String grade) {
studentGrades.add(Integer.parseInt(grade));
//System.out.println(studentGrades);
for (int i = 0;i < studentGrades.size(); i++){
//System.out.println(studentGrades.get(i));
}
}
public double askMedian() {
double median = 0;
for (int i = 0;i < studentGrades.size(); i++){
//System.out.println(studentGrades.get(i));
median += studentGrades.get(i);
sum = median/studentGrades.size();
}
//System.out.println(studentGrades);
//System.out.println(sum);
return sum;
}
}
FileVisitor.java:
package files;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
public class FileVisitor extends SimpleFileVisitor<Path> {
private List<Path> adresses = new ArrayList<>();
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
throws IOException {
return super.postVisitDirectory(dir, exc);
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
adresses.add(file);
//System.out.println(file);
return super.visitFile(file, attrs);
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc)
throws IOException {
return super.visitFileFailed(file, exc);
}
public List<Path> askFiles(){
return adresses;
}
}
任何帮助都会非常感激!几天来一直试图找出问题......