title: calculating average of a vector of doubles in java
body: <p>I'm having problems calculating the average of a vector of doubles. This is my code:</p>
  
  <pre><code>import java.util.Scanner;
  import java.util.Vector;
  public class MediaVettore {
  public static void main ( String [] args ) {
  System.out.println("Insert a sequence of double numbers (the program ends when the input is 0,0):");
  @SuppressWarnings("resource")
  Scanner input = new Scanner ( System .in );
  Vector &lt; Double &gt; v = new Vector &lt; Double &gt;();
  double n;
  double sum = 0;
  double average;
  do {
  n = input . nextDouble ();
  if(!v.isEmpty()) {
  for(int i=0; i&lt;=v.size()-1; i++) {
      sum += v.get(i);
    }
  }
    average=sum/v.size();
  if (n !=0) v. add (n);
  } while (n !=0);
  System.out.println("Average = " + average);
  System.out.println(v.size());
  System.out.println(sum);
  }
  }
  </code></pre>
  
  <p>It doesn't sum correctly, I don't understand why. Can you give me a little help please? <code>v.get(i)</code> should get the wanted double in the right position, isn't it?</p>
  
  <hr>
  
  <p>Edit for who need it:
  for-each version of the solution below</p>
  
  <pre><code>for(double i : v) {
      sum += i;
  }
  </code></pre>