I am trying to print the elements of an array into a table, but I'm having trouble formatting the array elements itself. Perhaps there is a better solution than what I'm currently using?
import java.util.Scanner;
public class RainFall{
public static void main(String args[]) {
//create rainfall array
double[][]rainfall = new double[2][3];
double sum = 0;
double average = 0;
int count = 0;
Scanner in = new Scanner (System.in);
System.out.println("Please enter rainfall for Region 1 and Region 2 one after the other");
//Create For loop to read in data
for(int i=0;i<rainfall.length;i++){
for(int j=0;j<rainfall[0].length;j++){
System.out.println("Enter rainfall:: ");
rainfall[i][j]=in.nextDouble();
}
}
//create for loop to sum rainfall
for (int i=0; i < rainfall.length; i++){
for (int j=0; j < rainfall[0].length; j++){
sum += rainfall[i][j];
count++;
}//end nested for
}//end outer for
//calculate average
average = sum/count;
//Print table
System.out.printf( "%5s %10s %15s %20s %25s %n", "Region 1"," Jan", "Feb", "Mar", "Avg Rainfall");
System.out.printf( "%d", rainfall[0][0] );
//Print average
System.out.println("The average rainfall is: " + average);
}
}