0

losing my mind looking for the issue with my method here. the goal is to take an arraylist of different times and amounts, turn it into a speed (time/amount) and then get the average speed of all the items in the arraylist, and continue to do that for each matching task in the arraylist.

here is my code:

 if(history.size()>0){

        for(CalcTasks c: calcTasks){
            String currentTask = c.getTask();

            double totalToAverage = 0;
            int counter = 0;

            for(WorkerHistory w: history){

                if(w.getTask().equals(currentTask)){
                    int time = (w.getTimeTaken());
                    if (time == 0){
                        continue;
                    }
                    int amount = w.getAmount();
                    double timePerAmount = (double) (time/amount+totalToAverage);
                    totalToAverage = timePerAmount;
                    counter++;

                    String strCount = Integer.toString(counter);
                    Log.d("myappcounter", strCount);
                }
            }

            if(counter != 0) {
                double averageMinutesPerAmount = totalToAverage / counter;

                double totalTaskTimeHours = ((c.getAmount() * averageMinutesPerAmount)/60d);

                calcTaskAverages.add(new CalcTaskAverages(currentTask, averageMinutesPerAmount, totalTaskTimeHours, c.getAmount()));

                String strAvg = Double.toString(calcTaskAverages.get(0).getAverageAnHour());

                Log.d("mytaskAverages",strAvg);

                double intermediate = totalTime + totalTaskTimeHours;
                totalTime = intermediate;
            }

        }

I have two 2 items in the arraylist. one is time=100, amount=100 and the other time= 100, amount = 200. this should give me an average speed of 0.75 but the log.d at the end is printing a average of 0.5. I'd appreciate any input you can give :)

  • 1
    I can't say I looked carefully at the full code, but did you mean for this: `double timePerAmount = (double) (time/amount+totalToAverage)`? I think its dividing time by amount and then adding `totalToAverage`. Is that the order you intended? – Tzvi2 Sep 18 '20 at 04:54
  • 1
    Further to the above comment, in `double timePerAmount = (double) (time/amount+totalToAverage);`, `time/amount` is an integer divided by an integer, which will always give an integer result. – Ken Y-N Sep 18 '20 at 04:57
  • ahhh so even though I cast it to a double it still gets truncated. Thanks Ken and Tzvi2. Saved me a lot of frustration – Trevor Soare Sep 18 '20 at 05:03

0 Answers0