Hi, ive been set an assignment for my java programming course and ive reached a point where i really cant work out the next step. Im wondering if anyone can help me with this code please. The assignment is as follows:
A file exists which contains the total rainfall for each month in a year, one double value on each line. Write a program which:
- Asks the user to type in the name of the file.
- Reads the data from that file storing each value in an Array.
- Iterates through the Array and prints out the total rainfall for the year.
- Iterates through the Array and prints out the average monthly rainfall.
- Iterates through the Array and prints out the month with the minimum rainfall and how much that was. (Printing the name of the month will earn extra marks).
- Iterates through the Array and prints out the month with the most rainfall and how much that was. (Again converting the month number to a String will earn extra marks).
So far i have done the first two steps and i almost have the code for the third step.
The data file im using looks something like this:
1.80 2.70 3.75 4.40 5.20 6.15 7.30 8.45 9.60 10.90 11.85 12.100
What i have done here is written:
1.80
2.70 etc meaning that the '1' will be January, the '2' will be February etc
So the number after the full stop is the rainfall. So i need to calculate the total of all the numbers on the right hand side of each full stop.
Then my code so far is this:
import java.util.*;
import java.io.*;
class Assignment5{
public static void main(String[] args)throws Exception{
String data;
ArrayList<Double> store = new ArrayList<Double>();
Scanner scanner = new Scanner(System.in);
System.out.println(" ");
System.out.println("Please enter the name of a file with .txt at the end.");
System.out.println(" ");
data = scanner.nextLine();
File inputFile = new File(data);
Scanner reader = new Scanner(inputFile);
while(reader.hasNext()){
store.add(reader.nextDouble());
}
calculateSum(store);
store.clear();
}
private static void calculateSum(ArrayList<Double> ArrayList){
double sum = 0;
double avg = 0;
double total = 0;
double totalrainfall = 0;
Iterator<Double> iterator = ArrayList.iterator();
while(iterator.hasNext()){
sum += iterator.next();
}
total = ArrayList.size();
avg = (sum / total);
System.out.println(" ");
System.out.println("The total rainfall is " + totalrainfall);
}
The part im stuck on is calculating the total rainfall. The data is a double value so i need to calculate all the integers after the full stop in the data file. I cant work out how to put this into my code. Thanks for helping in advance!