// This main method is supposed to load the values from a file into // a double array, then compute the average of the values in this array, writing // the average to an output file. Reorder the lines of this main method to // accomplish this. Put the main method in a class before running it. public static void main(String[ ] args) throws FileNotFoundException { double ave = (double) sum / count; pw.printf("Average: %.3f\n", ave); String line = s.nextLine( ); int sum = 0; int count = 0; for(int v : values) { PrintWriter pw = new PrintWriter("ave.txt"); sum += v; count++; } String fileName = "numbers.txt"; String[ ] inputs = line.split(","); Scanner s = new Scanner(f); for (int i = 0; i < len; i++) { values[i] = Integer.parseInt(inputs[i]); pw.close( ); } int len = inputs.length; File f = new File(fileName); int[ ] values = new int[len]; } ---------------------------------------------------------------- // Ans: Here are the reordered lines. // Try reordering the lines yourself before looking at the answer. import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Main { public static void main(String[] args) throws FileNotFoundException { File f = new File("numbers.txt"); Scanner s = new Scanner(f); String line = s.nextLine( ); String[ ] inputs = line.split(","); int len = inputs.length; int[ ] values = new int[len]; for (int i = 0; i < len; i++) { values[i] = Integer.parseInt(inputs[i]); } int sum = 0; int count = 0; for(int v : values) { sum += v; count++; } double ave = (double) sum / count; System.out.printf("Average: %.3f\n", ave); } } -------------------------------------------------------------- // Ans: Refactored source code with loadValuesFromFile // and computeAverage methods extracted. import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class Main { public static void main(String[] args) throws FileNotFoundException { String fileName = "numbers.txt"; int[ ] values = loadValuesFromFile(fileName); double ave = computeAverage(values); PrintWriter pw = new PrintWriter("ave.txt"); pw.printf("Average: %.3f\n", ave); pw.close( ); } public static double computeAverage(int[] values) { int sum = 0; int count = 0; for (int v : values) { sum += v; count++; } return (double) sum / count; } public static int[] loadValuesFromFile(String fileName) throws FileNotFoundException { File f = new File(fileName); Scanner s = new Scanner(f); String line = s.nextLine( ); String[ ] inputs = line.split(","); int len = inputs.length; int[ ] values = new int[len]; for (int i = 0; i < len; i++) { values[i] = Integer.parseInt(inputs[i]); } return values; } } ------------------------------------------------------