Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在从文件读取时尝试从另一个方法中的数组中获取最大值_Java_Arrays_List_File_Arraylist - Fatal编程技术网

Java 在从文件读取时尝试从另一个方法中的数组中获取最大值

Java 在从文件读取时尝试从另一个方法中的数组中获取最大值,java,arrays,list,file,arraylist,Java,Arrays,List,File,Arraylist,我有一个包含以下数据的文件(学生id、分数) 我试图实现的是使用ScoreApp类中的“readStudentData”方法读取它,但要以当前在该方法中打印的方式打印,但要通过printAll()方法完成。问题是我无法访问“readStudentData”中的“items”数组。除此之外,我无法通过“getMaxScore”方法获得最大分数 我的代码: import static java.lang.System.out; import java.io.BufferedReader; impor

我有一个包含以下数据的文件(学生id、分数)

我试图实现的是使用ScoreApp类中的“readStudentData”方法读取它,但要以当前在该方法中打印的方式打印,但要通过printAll()方法完成。问题是我无法访问“readStudentData”中的“items”数组。除此之外,我无法通过“getMaxScore”方法获得最大分数

我的代码:

import static java.lang.System.out;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class question5
{
  public static void main(String[] args) throws IOException
  {
      ScoreApp app = new ScoreApp("C:/Users/USER/workspace/Practical 2/Data/data");
      app.printAll();
      out.println(String.format("%50s", "").replace(' ', '-'));
      out.println("Max score: " + app.getMaxScore());
      out.println("Average score: " + app.getAverageScore());
      out.println("Number of pass scores: " + app.countPassScore());
      out.println("Total score (recursion): " +
      recursiveGetTotalScore(app.getStudents()));
      }
      static double recursiveGetTotalScore(List<Student> list)
      {
        return 0;
      }
      }
class ScoreApp
{
    public static List<Student> students;
    public List<Student> getStudents() //getter method
    {
    return students;
    }
    public ScoreApp(String data) throws IOException
    {
    students = new ArrayList<>();
    readStudentData(data);
    }
    private void readStudentData(String data) throws IOException
    {
        students = new LinkedList<Student>();
        Path path = new File(data).toPath();
        List<String> content = Files.readAllLines(path);
        for(String line : content){
            String[] items = line.split(",");
            String id = items[0];
            double score = Double.valueOf(items[1]);
            Student b = new Student(id, score);
            out.println(items[0]+": "+items[1]);
        }
    }
    public void printAll()
    {

    }
    public double getMaxScore()
    {
        double MaxScore = items[1];
        for(int i=1;i < items[1].length; i++)
            if(items[i] > MaxScore)
                MaxScore = items[i];
                out.println("Maximum score: " + MaxScore);
    }
    public double getAverageScore()
    {
        return 0;
    }
    public int countPassScore()
    {
        return 0;
    }
    }

class Student
{
private String ID;
private double score;
public Student(String ID, double score)
{
    this.ID = ID;
    this.score = score;
}
public String getID()
{
    return this.ID;
}
public void setID(String ID)
{
    this.ID = ID;
}
public double getscore()
{
    return this.score;
}
public void setscore(double score)
{
    this.score = score;
}
}
导入静态java.lang.System.out;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.IOException;
导入java.nio.file.Files;
导入java.nio.file.Path;
导入java.util.ArrayList;
导入java.util.LinkedList;
导入java.util.List;
导入java.util.Scanner;
公开课问题5
{
公共静态void main(字符串[]args)引发IOException
{
ScoreApp app=新的ScoreApp(“C:/Users/USER/workspace/Practical 2/Data/Data”);
app.printAll();
out.println(String.format(“%50s”和“-”).replace(“,”);
out.println(“最大分数:+app.getMaxScore());
out.println(“平均分数:+app.getAverageScore());
out.println(“通过分数的数量:+app.countPassScore());
out.println(“总分(递归):”+
递归GetTotalScore(app.getStudents());
}
静态双递归GetTotalScore(列表)
{
返回0;
}
}
班级计分应用程序
{
公开静态学生名单;
public List getStudents()//getter方法
{
留学生;
}
公共ScoreApp(字符串数据)引发IOException
{
学生=新数组列表();
readStudentData(数据);
}
私有void readStudentData(字符串数据)引发IOException
{
学生=新链接列表();
路径路径=新文件(数据).toPath();
列表内容=文件。readAllLines(路径);
for(字符串行:内容){
String[]items=line.split(“,”);
字符串id=项[0];
双倍分数=双倍价值(项目[1]);
学生b=新学生(id,分数);
out.println(项目[0]+“:”+项目[1]);
}
}
public void printAll()
{
}
公共双精度getMaxScore()
{
double MaxScore=项目[1];
对于(int i=1;iMaxScore)
MaxScore=项目[i];
out.println(“最大分数:+MaxScore”);
}
公共双getAverageScore()
{
返回0;
}
公共整数countPassScore()
{
返回0;
}
}
班级学生
{
私有字符串ID;
私人双积分;
公立学生(字符串ID,双倍分数)
{
this.ID=ID;
这个分数=分数;
}
公共字符串getID()
{
返回此.ID;
}
公共无效集合ID(字符串ID)
{
this.ID=ID;
}
公众双倍得分()
{
返回此.score;
}
公开考试分数(双倍)
{
这个分数=分数;
}
}

您的ScoreApp课程中已有一个学生名单。在“
readStudentData
”方法中,您应该在该列表中填充/插入数据,在“
getMaxScore()
”方法中,您应该使用该列表

public double getMaxScore() {
    double maxScore = 0;
    //"students" is the class variable / list you have in your ScoreApp class
    for(Student current: students) {
        double currentScore = current.getScore();
        if(currentScore > maxScore)
            maxScore = currentScore;
    }
    return maxScore;
}
更新:平均分数

public double getAverageScore() {
    double averageScore = 0;
    int noOfStudents = 0;
    int sumOfScores = 0;

    for(Student current: students) {
        double currentScore = current.getScore();
        sumOfScores += currentScore;
        noOfStudents++;
        averageScore = sumOfScores / noOfStudents;
    }
    return averageScore;
}

readStudentData
is void打印出数据是毫无意义的。不要让它打印任何内容,而是让它返回一个学生列表。或者甚至让它仍然不返回任何内容,因为它填充了一个成员变量。在
printAll
中,迭代
ScoreApp.students
并打印每个学生(或者让他们自己打印…
for(Student-Student:students){Student.print()}
并记住在
readStudentData
中的for循环中向
students
添加
b
,我刚刚尝试了@Tibrogargan,但是通过这样做了out.println(学生);“在printAll()下,我只剩下它打印了”Student@4e25154f“等等,然后给它一个像样的
toString()
method。这非常有用,我该如何获得平均分数?请遵循Java命名约定:变量名以小写字母开头感谢您的输入!我只是想让事情与他的文件的其余部分保持一致。无论如何,我已经更新了答案。
public double getAverageScore() {
    double averageScore = 0;
    int noOfStudents = 0;
    int sumOfScores = 0;

    for(Student current: students) {
        double currentScore = current.getScore();
        sumOfScores += currentScore;
        noOfStudents++;
        averageScore = sumOfScores / noOfStudents;
    }
    return averageScore;
}