java 未找到文件异常:系统找不到指定的文件

lokaqttq  于 2023-03-16  发布在  Java
关注(0)|答案(1)|浏览(192)

我的任务是创建一个程序来执行一个班级的学生成绩的统计分析。这个班级最多可以有40名学生。在学期中有五次测验。每个学生由一个四位数的学生ID号来识别。这个程序是打印学生成绩,并计算和打印每个测验的统计数据。我们打算使用下面提供的4个不同的班级

package lab2;
import java.util.Arrays;
public class Student {
    
    private int SID;
    private int[] scores = new int[5];
    public int getSID() {
        return SID;
}

        public void setSID(int SID) {
                this.SID = SID;
        }
            public int[] getScores() {
                return scores;
            }
            public void setScores(int[] scores) {
                this.scores = scores;
            }   
            public void print() {
                System.out.println(SID + " " + Arrays.toString(scores));
            }
    }
package lab2;
import java.util.Arrays;
    public class Statistics {
        
        private int [] lowscores = new int [5];
        private int [] highscores = new int [5];
            private float [] avgscores = new float [5];
            
        void findlow(Student [] a){
            
    for (int i = 0; i < lowscores.length; ++i) {
        lowscores[i] = 100;
        for (Student student : a) {
            if (student.getScores()[i] < lowscores[i]) {
                lowscores[i] = student.getScores()[i];
            }
        }
    }
}
        void findhigh(Student [] a){
            
            for (int i = 0; i < highscores.length; ++i) {
                highscores[i] = 0;
                for (Student student : a) {
                    if (student.getScores()[i] > highscores[i]) {
                        highscores[i] = student.getScores()[i];
                    }
                }
            }
        }
        void findavg(Student [] a) {
            for (int i = 0; i < avgscores.length; ++i) {
                avgscores[i] = 0;
                for (Student student : a) {
                    avgscores[i] += student.getScores()[i];
                }
                avgscores[i] /= a.length;
            }
        }
        public void print() {
            System.out.println("High Score " + Arrays.toString(highscores));
            System.out.println("Low Score " + Arrays.toString(lowscores));
            System.out.println("Average " + Arrays.toString(avgscores));
        }
    }
package lab2;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Util {

   public static Student[] readFile(String StudentScores, Student[] stu) {
     
        int i = 0;

        //Reads the file and builds student array.
        try { FileReader File = new FileReader(StudentScores);
        BufferedReader buff = new BufferedReader(File);

            // skipping header row
            boolean eof = buff.readLine() == null;

            while (!eof) {
                String line = buff.readLine();

                if (line == null)
                    eof = true;

                else {
                    String[] data = line.split(" ");
                    stu[i] = new Student();
                    stu[i].setSID(Integer.parseInt(data[0]));

                    int[] scores = new int[data.length - 1];

                    for (int j = 1; j < data.length; ++j) {
                        scores[j - 1] = Integer.parseInt(data[j]);
                    }
                    stu[i].setScores(scores);
                    ++i;
                }
            }

            buff.close();

        } catch (IOException e) {
            System.out.println("Error -- " + e.toString());
        }

        Student[] students = new Student[i];
        System.arraycopy(stu, 0, students, 0, i);
        return students;
    }
}
package lab2;

import java.io.FileNotFoundException;

public class Driver {
    
     public static void main(String[] args) throws FileNotFoundException {
        
    
     Student[] lab2 = new Student[40];
     // Populate the student array
     lab2 = Util.readFile("filename.txt", lab2);
     Statistics statlab2 = new Statistics();
     statlab2.findlow(lab2);
     statlab2.findhigh(lab2);
     statlab2.findavg(lab2);
     System.out.println("Stud Qu1 Qu2 Qu3 Qu4 Qu5");
     for (Student student : lab2) {
     student.print();
     }
     statlab2.print();
     }
    }

输入应该从文本文件读取。我有一个名为StudentScores.txt的文件,但当我尝试运行它时,我收到此消息。我不确定从这里转到哪里或如何解决此问题。如果能提供帮助,我将不胜感激。

Error -- java.io.FileNotFoundException: StudentScores.txt (The system cannot find the file specified)
Stud Qu1 Qu2 Qu3 Qu4 Qu5
High Score [0, 0, 0, 0, 0]
Low Score [100, 100, 100, 100, 100]
Average  [NaN, NaN, NaN, NaN, NaN]

'

6ojccjat

6ojccjat1#

从错误中:
Error -- java.io.FileNotFoundException: StudentScores.txt (The system cannot find the file specified)
它指出目标文件未被读取。
从下面的代码中,它尝试读取文件filename.txt

lab2 = Util.readFile("filename.txt", lab2);

您必须确保在工作目录中或在与可执行文件相同的目录中有一个名为filename.txt的文件。
或者,您可以尝试先用filename.txt的确切路径替换filename.txt

lab2 = Util.readFile("C:/.../.../filename.txt", lab2);

相关问题