Im stuck. Here are the requirements. Please help.
write a program to print out the percent of the total rainfall that fell on each month.
To do this, you will need to set up a new array of doubles, say, “percent”.
To compute the percent value for a particular month, take the rainfall amount for that month divide
it by the total yearly rainfall and multiply it by 100.
Do this for each month using a loop.
Then modify the code to print out the monthly percent values too.
CODE
package cs162ic4;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.FileReader;
/**
* Reads in a file and computes the total and average rainfall information
* @authorJonathan Furst
*
*/
public class Main {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
final String INPUTFILE = "src/cs162ic4/rainfall.txt";
/*
* Set up everything needed to read a file
* See pp. 147-158 in book.
*/
Scanner inFile = new Scanner( new FileReader( INPUTFILE ) );
String header = inFile.nextLine();
/*
* Read in the file of rainfall data
*/
final int NUM_MONTHS = 12;
double[] data = new double[ NUM_MONTHS ];
for (int i=0; i<NUM_MONTHS; i++){
data[i] = inFile.nextDouble();
}
inFile.close();
/*
* Compute total and average
*/
double total = 0.0;
for (int i=0; i<data.length; i++)
total = total + data[i];
double average = total/data.length;
double total = 0.0;
double [] percent;
percent = new double [ 13 ];
for(double i=0; i<percent.length; i++)
/*
* Print results
*/
System.out.println( header );
for (int i=0; i<data.length; i++)
System.out.printf(" %.2f inches%n", data[i] );
System.out.printf(" Total: %.2f inches%n", total );
System.out.printf(" Average: %.2f inches%n", average );
}
}
text document
Monthly Rainfall
0.1
0.05
0.7
2.0
7.8
4.7
2.1
0.2
1.2
3.1
0.2
0.05