title: Android/Java: Code to calculate the average of values in an arrayList?
body: <p>In my application I am reading an external value (int) from an EEG headset. </p>
  
  <p>I thought that the value was read once a second but from my logcat report it seems that it is being read more than that. </p>
  
  <p>This is disrupting my intentions as I want to calculate the average of the values. I am trying to do so using an increment operator called count. </p>
  
  <p>Note that the values being read from the headset can only be from 1-100, so the output for average has to be incorrect as it is 168.</p>
  
  <p>What am I doing wrong?</p>
  
  <p><strong>Current code:</strong></p>
  
  <p>Where value is being recorded:</p>
  
  <pre><code>case TGDevice.MSG_MEDITATION:
  
                  //BELOW CODE RELATES TO SAVING AN AVERAGE VALUE
  
                  meditationValues.add(msg.arg1); 
                  count++;
  
                  for(int i = 0; i &lt; meditationValues.size(); i++){
  
                      totalofMedLevels += meditationValues.get(i);
  
                      Log.d("Total", "Total : " + totalofMedLevels);
                      Log.d("Count ", "Count: " + count);
  
                  }
  </code></pre>
  
  <p>Calculating average:</p>
  
  <pre><code>public void getAverageValue(){
              //stop device so that the values can be recorded
              device.stop();
  
  
              averageMedLevel=totalofMedLevels/count; 
  
              Log.d("Average level:", "Average: " + averageMedLevel);
  
          }
  </code></pre>
  
  <p><strong>Logcat Output:</strong></p>
  
  <p>For total:</p>
  
  <pre><code>07-14 14:16:44.312: D/Total(30422): Total : 301
  07-14 14:16:45.313: D/Total(30422): Total : 301
  07-14 14:16:45.313: D/Total(30422): Total : 301
  07-14 14:16:45.313: D/Total(30422): Total : 301
  07-14 14:16:45.323: D/Total(30422): Total : 301
  07-14 14:16:45.323: D/Total(30422): Total : 301
  07-14 14:16:45.323: D/Total(30422): Total : 301
  07-14 14:16:45.323: D/Total(30422): Total : 342
  07-14 14:16:45.323: D/Total(30422): Total : 406
  07-14 14:16:45.323: D/Total(30422): Total : 456
  07-14 14:16:45.323: D/Total(30422): Total : 496
  07-14 14:16:46.314: D/Total(30422): Total : 496
  07-14 14:16:46.314: D/Total(30422): Total : 496
  07-14 14:16:46.314: D/Total(30422): Total : 496
  07-14 14:16:46.314: D/Total(30422): Total : 496
  07-14 14:16:46.314: D/Total(30422): Total : 496
  07-14 14:16:46.314: D/Total(30422): Total : 496
  07-14 14:16:46.324: D/Total(30422): Total : 537
  07-14 14:16:46.324: D/Total(30422): Total : 601
  07-14 14:16:46.324: D/Total(30422): Total : 651
  07-14 14:16:46.324: D/Total(30422): Total : 691
  07-14 14:16:46.324: D/Total(30422): Total : 725
  07-14 14:16:47.285: D/Total(30422): Total : 725
  07-14 14:16:47.285: D/Total(30422): Total : 725
  07-14 14:16:47.285: D/Total(30422): Total : 725
  07-14 14:16:47.285: D/Total(30422): Total : 725
  07-14 14:16:47.285: D/Total(30422): Total : 725
  </code></pre>
  
  <p>For count:</p>
  
  <pre><code>07-14 14:16:47.285: D/Count(30422): Count: 12
  07-14 14:16:47.285: D/Count(30422): Count: 12
  07-14 14:16:47.285: D/Count(30422): Count: 12
  07-14 14:16:47.285: D/Count(30422): Count: 12
  07-14 14:16:47.285: D/Count(30422): Count: 12
  07-14 14:16:47.295: D/Count(30422): Count: 12
  07-14 14:16:47.295: D/Count(30422): Count: 12
  07-14 14:16:47.295: D/Count(30422): Count: 12
  07-14 14:16:47.295: D/Count(30422): Count: 12
  07-14 14:16:47.295: D/Count(30422): Count: 12
  07-14 14:16:47.295: D/Count(30422): Count: 12
  07-14 14:16:48.286: D/Count(30422): Count: 13
  07-14 14:16:48.286: D/Count(30422): Count: 13
  07-14 14:16:48.286: D/Count(30422): Count: 13
  07-14 14:16:48.296: D/Count(30422): Count: 13
  07-14 14:16:48.296: D/Count(30422): Count: 13
  07-14 14:16:48.296: D/Count(30422): Count: 13
  07-14 14:16:48.296: D/Count(30422): Count: 13
  07-14 14:16:48.296: D/Count(30422): Count: 13
  07-14 14:16:48.296: D/Count(30422): Count: 13
  07-14 14:16:48.296: D/Count(30422): Count: 13
  07-14 14:16:48.296: D/Count(30422): Count: 13
  07-14 14:16:48.296: D/Count(30422): Count: 13
  </code></pre>
  
  <p>For average:</p>
  
  <pre><code>07-14 14:16:53.281: D/Average level:(30422): Average: 168
  </code></pre>
  
  <p>EDIT (TRYING TO ONLY SAVE A VALUE IF IT IS ABOVE 0):</p>
  
  <pre><code>if((msg.arg1)!=0){
  
                      meditationValues.add(msg.arg1); 
  
                  }
  
  
  
                  for(int i = 0; i &lt; meditationValues.size(); i++){
  
                      //totalofMedLevels += meditationValues.get(i); 
                      totalofMedLevels+=msg.arg1;
                      count++; 
  
                      Log.d("Total", "Total : " + totalofMedLevels);
                      Log.d("Count ", "Count: " + count);
  
                  }
  </code></pre>