1
\$\begingroup\$

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);
  }
}
\$\endgroup\$
0

2 Answers 2

3
\$\begingroup\$

If the array bounds are fixed, then there's no need for the count variable, since its value will always be the same. There's also no need to run a separate loop for the summation. You can do this at the same time as you're gathering the input. And since your output shows that you want to show the averages by region, you'll need to have somewhere to store those values, too.

Also, try using more descriptive prompts for the user so they know how many inputs are expected.

So the first part of your function becomes

//could also define these as constants
int regions = 2;
int months = 3;
int count = regions * months;

double[][]rainfall = new double[regions][months];
double[] avgByRegion = new double[regions];
double sum = 0;
double average = 0;

Scanner in = new Scanner (System.in);

//Create For loop to read in data
for(int i=0; i < regions; i++){
  double sumByRegion = 0;
  for(int j=0; j < months; j++){
    System.out.printf("Enter rainfall for Region %d, Month %d:: ", i+1,j+1);
    rainfall[i][j]=in.nextDouble();
    sumByRegion += rainfall[i][j];
  }
  avgByRegion[i] = sumByRegion/months;
  sum += sumByRegion;
}

//calculate average
average = sum/count;

Then you can format your output with another set of loops:

for(int i=0; i < regions; i++){
  System.out.printf("Region %d\t  Jan\t  Feb\t  Mar\t  Avg Rainfall%n", i+1);
  for(int j=0; j < months; j++){
    System.out.printf("\t%2.1f",rainfall[i][j]);
  }
  System.out.printf("\t%2.1f%n", avgByRegion[i]);
}
System.out.println("The overall average rainfall is: " + average);
\$\endgroup\$
2
\$\begingroup\$

You cannot use %d for double values.Try something like %3.2f for printing the rainfall values if you want to avoid the unnecessary precision.I am not sure why you are trying to print only one value while you have listed three months of rainfall.Try something like:

 System.out.printf( "\t%3.2f\t%3.2f\t%3.2f", rainfall[0][0],rainfall[0][1],rainfall[1][0] );
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.