package sales;
import java.io.File;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in); //what is this?
System.out.println("Enter file name: ");
String filename = scan.nextLine();
scan.close();
File myFile = new File(filename);
scan = new Scanner(myFile);
// yearly sum for all branches
double yearlySum = 0;
//quarterly sum for all branches
//initialised to 0 by default
double[] quarterlySum = new double[4]; //??
int count = 0;
while(scan.hasNextDouble()) {
double sales = scan.nextDouble();
yearlySum += sales;
//quarterly 0, 1, 2 and 3
int quarter = count % 4;
quarterlySum[quarter] += sales;
count++;
}
scan.close();
System.out.println(yearlySum);
for (int i = 0; i < 4; i++) {
System.out.println(quarterlySum[i]);
}
}
}
this is the TXT I am using
1 1274.0 1585.0 1820.5 2038.4
2 1164.0 897.2 1432.8
3 987.4 1106.7 897.5 2137.2
this is the console
15346.7
4369.7
3416.2
3736.4
3824.3999999999996
I have copied this code from a tutorial so am unsure about a few things
the first double on the console should I’m assuming be the sum of all doubles from the TXT, which would be under the variable yearlySum. According to my calculations this would be 15,340.7 NOT 15346.7
the first of the array which I’m assuming adds all three first index’s of the lines should be 3,425.4 NOT 4369.7
the next should be 3,588.9 NOT 3416.2
the next should be 4,150.8 NOT 3736.4
the next should be 4,175.6 NOT 3824.3999999999996
Whats the problem with the code?
this is the tutorial I followed though the TXT file is a little different
Any help would be greatly appreachiated
I tried changing TXT numbers which did not have a decimal point to have a .0