Hey, guys I am fairly new to java so help would more than appreciated. I have initialized and filled a 2D array and also two 1D arrays. I have them running for the time being as I can print them to my output. My next problem here is finding and printing the maximum value in the 2D array. The 2D array is named precipitationAmount. This array consists of 5 rows and 12 columns. ( 5 years and 12 months for each year). This is my expected output: The maximum rainfall of 6.44 occurred in May of 1995 I know this is not right, but i think i am somewhat close? maybe not. Help would be greatly appreciated. Sorry if this is unclear or hard to deal with as I am new to Java and this website. thanks.
PS. I am using the program called eclipse to compile and run my code.
Not sure if there is a cap for characters in posting, I'll try posting the whole thing. There is also a read in IO file that fills the array.
public class Precipitation { private final static int MONTHS = 12; private final static int YEARS = 5; private final static int STARTYEAR = 1995; private final String[] monthLabel = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; double [] [] precipitationAmount; double [] yearTotal; double [] monthlyAverage; //DECLARE ARRAYS HERE FOR TASK #1 public Precipitation() { precipitationAmount = new double [5] [12]; yearTotal = new double [12]; monthlyAverage = new double [12]; //CREATE THE ARRAYS OF THE CORRECT SIZE HERE FOR TASK #1 //(USE THE CLASS CONSTANTS) } public void readFile(Scanner infile) throws IOException { for(int row = 0; row< precipitationAmount.length; row++) { for(int col = 0 ; col<this.precipitationAmount[row].length;col++) { double value = infile.nextDouble(); System.out.print("[ row:"+row+"col:"+col+" value: "+value+" ] "); precipitationAmount [row] [col] = value; } System.out.println(); } //CREATE A LOOPING STRUCTURE TO READ DATA FROM THE FILE AND //STORE IT INTO THE 2-D ARRAY FOR TASK #2 infile.close(); calculateMonthlyAverage(); calculateYearTotal(); } private void calculateMonthlyAverage() { double month = 0; for(int col = 0; col < MONTHS; col++) { for(int row = 0 ; row < YEARS; row++) { month = month + precipitationAmount[row][col]; } monthlyAverage[col] = month /5; month = 0; System.out.println(monthlyAverage[col]); } } //CREATE A LOOPING STRUCTURE TO CALCULATE THE MONTHLY AVERAGE //FOR EACH COLUMN AND STORE IT INTO THE MONTHLY AVERAGE ARRAY //FOR TASK #2 private void calculateYearTotal() { double year = 0; for(int row = 0; row < YEARS; row++) { for(int col = 0 ; col < MONTHS; col++) { year = year + precipitationAmount[row][col]; } yearTotal[row] = year; year = 0; System.out.println(yearTotal[row]); } } //CREATE A LOOPING STRUCTURE TO CALCULATE THE YEAR TOTAL //FOR EACH ROW AND STORE IT INTO THE YEAR TOTAL ARRAY //FOR TASK #2 public String findMax() { String month = ""; double max = 0; double value = 0; for(int row = 0; row < precipitationAmount.length; row++) { for(int col = 0; col < precipitationAmount[row].length; col++); { if(max < value) { max = value; monthLabel[row]=month; System.out.println("The maximum rainfall of"+max+ "occurred in"+month+"of"+STARTYEAR); } } } //TASK #3A //SEARCH THE ARRAY FOR THE LARGEST AMOUNT OF PRECIPITAION //SAVE THE AMOUNT, MONTH, AND YEAR. return "The maximum rainfall"; //RETURN A STRING CONTAINING INFORMATION ABOUT THE MAXIMUM PRECIPITATION }