Home > Enterprise >  None of my array calculation methods are working / returning values
None of my array calculation methods are working / returning values

Time:01-31

I'm very new to Java. I'm trying to build a really simple departmental sales reporting system for my portfolio that declares 5 arrays (of length 12, so for a 12-month reporting period) for each department. I have an addData method that manually adds test data into each array. The methods that calculate and return the Q1-Q4 sales data for each department.

The problem I have is that while the arrays are storing the test data the methods don't seem to be returning any values so I'm getting 0.0 (there are no error messages). Can someone help me to fix this? The code is below:

public double q3Sales (double [] deptArray)
{
    //9.Q3 Sales Calculation for all departments
    deptArray = new double[monthly];
    double Q3Sum = 0;
    
    Q3Sum = deptArray[6] deptArray[7] deptArray[8];
    return Q3Sum;
}

public static void main(String[] args) 
{
    Quarterly_Report n=new Quarterly_Report();
        n.addData();
        
        //11. Display Quarterly Sales Table in the Eclipse Console
        
        System.out.println("------------");
        System.out.println("QUARTERLY DEPARTMENTAL SALES REPORT");
        System.out.println("------------");
        System.out.println(n.q1Sales(n.electrical));
        
        System.out.println("        \t Electrical \t Kitchen \t Bathroom \t Soft Furnishings \t Accessories");
    
        
        System.out.print("Q3 Sales");
        System.out.print("\t"  "\t");
        System.out.print(n.q3Sales(n.electrical));
        System.out.print("\t"   "\t");
        System.out.print(n.q3Sales(n.kitchen));
        ... etc
    
  1. I am expecting the correct, calculated Q3Sum figure to be displayed. But it just shows 0.0. No errors displayed so I don't know how to go about and fix this as it seems the calculation just isn't being performed.
  2. I have tried using 2D arrays but they are more complex than my ability at the moment.
  3. I've tried using a for loop in q3Sales() but I get the same problem.

CodePudding user response:

In your q3Sales-method you are reassigning the paramter array. When instatiated, arrays are always filled with 0 (0.0 as double or false as boolean).

CodePudding user response:

You're overwriting the deptArray in the method, which is wrong. Instead you should use the array passed to the method:

public double q3Sales (double [] deptArray)
{
    //9.Q3 Sales Calculation for all departments
    return deptArray[6] deptArray[7] deptArray[8];
}
  • Related