Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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);
  }
}
share|improve this question

migrated from stackoverflow.com Feb 12 at 19:12

This question came from our site for professional and enthusiast programmers.

2 Answers 2

up vote 3 down vote accepted

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);
share|improve this answer

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] );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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