For an assignment in my CS class I am having to take in an input of floats, save them in an array, then display them and add them together to get a sum of the floats. Currently I am having an issue with getting the sum of the floats.
As far as I can tell the below code should work, but I get an error: "can not add an object and an int".
My code is:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner; // load the scanner utility
class Lab7 {
public static void main(String[] args) {
double n;
double s;
Scanner input = new Scanner( System.in ); //define the scanner
List L = new ArrayList();
s=0.0;
n=1.0;
// read in the floats
while ( n != 0.0 )
{
System.out.println("Please input a number");
n = input.nextFloat();
if ( n != 0.0) L.add(n);
System.out.println("read in " + n);
}
for (int i=0; i< L.size(); i= i+1)
{
System.out.println("List contains " + L.get(i));
s = s + L.get(i);
System.outprintln("Sum of nunbers " + s);
}
}// of main
} // of Lab7