我正在为学校做一项评论作业。这个任务是写一个类,从标准输入读取一个包含多个整数的文件,并将它们放入一个数组中。然后需要编写方法来查找平均值、中位数、最大值、最小值和标准差。
文件内容如下:
45
56
67
78
89
等等...
因此,我认为我需要创建一个数组列表(因为长度未知),并使用扫描器读取每一行的整数,然后创建所需的方法。 然而,我不明白如何正确地在FileReader和Scanner之间使用。 我目前正在运行BlueJ。文本文件位于项目文件夹下,但代码无法找到该文件。
以下是我目前的进展。
import java.io.*;
import java.util.*;
import java.math.*;
public class DescriptiveStats
{
public DescriptiveStats(){}
public FileReader file = new FileReader("students.txt");
public static void main(String[] args) throws IOException
{
try{
List<Integer> scores = new ArrayList<Integer>();
Scanner sc = new Scanner(file);
while(sc.hasNext())
{
scores.add(sc.nextInt());
}
sc.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}