Question: You will be provided with a text file that has a list of ratings (values between 1 and 10) with multiple values on a line
You will be provided with a text file that has a list of ratings (values between 1 and 10) with multiple values on a line and multiple lines. For your testing, just create one with numbers of your choice. You need to write a program named Distribution.java that will read in the data and return a frequency distribution and an average value for the data. NOTE: you must let the user choose the file from a FileDialog box. Do not hard code the path location!
For instance, if the file had the following
1 6 8 7
2 5 8 9 9
3 4
2 3 9 9 8 8 7
10 1 10 10
3
I began the code, getting a red x on valuesArray[t]=i;
public class Distribution {
public static void main(String[] args) { double[] values = new double [11]; distribution(values); average(values);
} public static int[] readText() { //create an array int[] values = new int[11]; Frame f = new Frame(); //decide from where to read the file FileDialog foBox = new FileDialog(f,"Reading text file", FileDialog.LOAD); foBox.setVisible(true); //get the absolute path to the file String foName = foBox.getFile(); String dirPath = foBox.getDirectory(); // create a file instance for the absolute path File inFile = new File(dirPath + foName); BufferedReader in=null; try { // create a BufferedReader to use to read in the file in= new BufferedReader(new FileReader(inFile)); // read in the first entire line from the file String line = in.readLine(); // continue until the line is null (ie you are at the end of the file) while(line!=null) { // create a StringTokenizer instance that will break the line apart at each | symbols // NOTE: the second parameter is the symbols used to separate fields StringTokenizer t = new StringTokenizer(line," "); String[] valuesArray = new String [11]; String i = t.nextToken(); while(t.hasMoreTokens()){ valuesArray[t]=i; } //add to array list //valuesArray[t]++; line=in.readLine(); } } // catch any IOException that occurs catch (IOException io) { System.out.println("An IO Exception occurred"); io.printStackTrace(); } finally // finally always runs no matter what so close the file here! { // close the file. Java is neurotic - it worried "but what if it is already closed?" so needs another try/catch try{ in.close(); } catch (Exception e) {} // note the {} - means "do nothing". I wanted it closed anyway. } } public static void distribution (double[] valuesArray){ System.out.println("The following is the distribution of values:"); for (int i=1;i }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
