Question: Please find and fix the error in the code for Java Eclipse please rewrite code if necessary import java.io.*; import java.util.Arrays; import java.util.Scanner; public class

Please find and fix the error in the code for Java Eclipse please rewrite code if necessary

import java.io.*; import java.util.Arrays; import java.util.Scanner;

public class tank { public static void main(String[] args) { int[][] data = null; try (Scanner sc = new Scanner(new File("input.txt"))) { int row = 0; while (sc.hasNextLine()) { String line = sc.nextLine(); String[] values = line.split(","); if (data == null) { data = new int[values.length][values.length]; } for (int col = 0; col < values.length; col++) { data[row][col] = Integer.parseInt(values[col]); } row++; } } catch (FileNotFoundException e) { System.out.println("File not found!"); } farm_avg(data); month_avg(data); top_farms(data); top_months(data); }

private static void top_months(int[][] data) { // TODO Auto-generated method stub }

public static void farm_avg(int[][] data) { for (int row = 0; row < data.length; row++) { int sum = 0; for (int col = 0; col < data[row].length; col++) { sum += data[row][col]; } int avg = sum / data[row].length; System.out.println("Sum of farm"+row+" is: "+sum); System.out.println("Average production for farm " + row + " is: " + avg); } }

public static void month_avg(int[][] data) { for (int col = 0; col < data[0].length; col++) { int sum = 0; for (int row = 0; row < data.length; row++) { sum += data[row][col]; } int avg = sum / data.length; System.out.println("Average production for month " + col + " is: " + avg); } } public static void top_farms(int[][] data) { } public static void top_farms1(int[][] data) { int[] farm_production = new int[data.length]; for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[i].length; j++) { farm_production[i] += data[i][j]; } }

int[] sorted_farm_production = farm_production.clone(); Arrays.sort(sorted_farm_production);

int[] top_three_farms = new int[3]; for (int i = 0; i < 3; i++) { top_three_farms[i] = sorted_farm_production[sorted_farm_production.length - 1 - i]; }

System.out.println("Farm with the highest production: " + indexOf(farm_production, top_three_farms[0]) + " with production: " + top_three_farms[0]); System.out.println("Farm with the second highest production: " + indexOf(farm_production, top_three_farms[1]) + " with production: " + top_three_farms[1]); System.out.println("Farm with the third highest production: " + indexOf(farm_production, top_three_farms[2]) + " with production: " + top_three_farms[2]); }

public static int indexOf(int[] arr, int value) { for (int i = 0; i < arr.length; i++) { if (arr[i] == value) { return i; } } return -1; }} input file

15, 50, 62, 72, 73, 74, 80, 55, 46, 43, 34, 19

58, 60, 70, 73, 76, 82, 87, 65, 51, 42, 26, 19

43, 59, 65, 72, 82, 65, 64, 53, 51, 41, 27, 22

18, 42, 63, 69, 73, 85, 84, 65, 44, 38, 27, 14

19, 46, 50, 77, 82, 95, 98, 93, 84, 74, 17, 10

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!