I'm working on a review assignment for school. The assignment is to write a class that reads from standard input a file containing several integers, which are to be put into an array. From here, methods need to be written that find the average, median, max, min, and standard deviation.
It reads like so:
45
56
67
78
89 etc...
So, I'm assuming I need to create an array list (since the length is undefined) and use scanner to read each line for an integer, then create the methods that will pick apart what I need. However, I fail to understand how to properly use FileReader and Scanner in conjunction. I'm currently running BlueJ. The text file is located under the project folder, yet the file is never found by the code.
Here is what I have so far.
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(); } }