0
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

https://www.youtube.com/watch?v=PJ1x5gcAoYs&list=PLtQo0sxRN7JKKla3_GAF05dySjyy3nINa&index=23&ab_channel=BeginCodingFast

Any help would be greatly appreachiated

I tried changing TXT numbers which did not have a decimal point to have a .0

11
  • 11 values in the file Commented May 13, 2024 at 20:31
  • 2
    the values 1, 2 and 3 are probably not intended to be in the file, just displayed line numbers (1+2+3=6 the sum difference) ||| also, apparently the second line is missing the fourth value (posted code is not considering lines) Commented May 13, 2024 at 20:51
  • @g00se 14 values though Commented May 13, 2024 at 20:53
  • (as often, video is not the best format for programming tutorials; IMO text is better) Commented May 13, 2024 at 20:58
  • @Siguza I'm assuming the integers are line numbers Commented May 13, 2024 at 21:14

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.