import java.util.Scanner; public class SumArray2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; double[] a; System.out.print("How many numbers in input: "); n = in.nextInt(); // n = # values a = new double[n]; // Create an array of n elements int i; /* ------------------ Read in the values ------------------ */ for ( i = 0; i < a.length; i++ ) { System.out.print("Enter a number: "); a[i] = in.nextDouble(); // Read in number } /* ------------------ Compute the sum ------------------ */ double sum; sum = 0.0; for ( i = 0; i < a.length; i++ ) { sum = sum + a[i]; // Add a[i] to the running sum // Common error: sum = a[i] // This will store a[i] into sum } System.out.println( sum ); } }