1

I'm creating a program that stores doubles in an array and then stores each array in an ArrayList then calculates the average from that ArrayList, but I keep getting a "possible lossy conversion from double to int." in line 5.

I'm new to java so I might be overseeing a simple fix.

public static double calculateAll(List<double[]> allNumbers) {  
    double average = 0.0;
    double total = 0.0;
    for(int i = 0; i < allNumbers.size(); i++) {
        total += allNumbers.get(i);
    }
    average = total/allNumbers.size();
        return average;
}
bloocool
  • 43
  • 7
  • `for(double i = 0.0` --> change it to `for(int i = 0` – vinS Dec 10 '18 at 22:03
  • Why do you use a double for the variable i? It's an index in a list. Indices in a list are integers, not doubles. – JB Nizet Dec 10 '18 at 22:03
  • @vinS might aswell use a foreach whole ur at it – SamHoque Dec 10 '18 at 22:04
  • `List` are you SURE you wanted this? This is creating a Collection that holds double arrays. – SamHoque Dec 10 '18 at 22:04
  • 1
    `allNumbers.get(i)` return an array. `total += allNumbers.get(i);` doesn't make sense. – shmosel Dec 10 '18 at 22:07
  • @SamzSakerz I'm pretty sure... Like I said I'm new so I might be going at it the wrong way. I'm trying to get the average of all of the list's doubles and calculate the average. – bloocool Dec 10 '18 at 22:13
  • @achAmháin I tied correcting it but I now get incompatible types between 'total' and 'allNumbers'. "First type: double second type double[]" – bloocool Dec 10 '18 at 22:16
  • Look at my answer, You can do it in 1 line via java 8. – SamHoque Dec 10 '18 at 22:22
  • @bloocool for that latest issue, just replace `average = total/allNumbers.size();` by `average = total/(allNumbers.size());` and it should work. – Anis R. Dec 10 '18 at 22:22
  • @AnisR. The OP is overcomplicating this, I have already provided an answer with streams that can do this in 1 line. – SamHoque Dec 10 '18 at 22:23
  • @SamzSakerz good answer, much simpler indeed, but I was just replying to bloocool's last comment. – Anis R. Dec 10 '18 at 22:27
  • @SamzSakerz Oh man, thanks! I'll give this a shot. I needed to refresh the page. Yeah, like I said, "I'm new to java so I might be overseeing a simple fix." Thanks for helping me out. – bloocool Dec 10 '18 at 22:30
  • @bloocool You can either use to `foreach` loop or the `stream` How ever stream requires java 8. – SamHoque Dec 10 '18 at 22:32

1 Answers1

0

I am not sure what you are asking in the question, but I think this is what you are looking for.

public static double calculateAll(List<Double> allNumbers) {
    double average;
    double total = 0.0;
    for (Double allNumber : allNumbers) {
        total += allNumber;
    }
    average = total / allNumbers.size();
    return average;
}

You were storing an Array Inside a Collection. So I have changed that to Double note that double is native while Double isn't. You can't have double inside a Collection. and then I have converted the for loop into a foreach loop

Here is how you can call this code.

public static void main(String[] args) {
    List<Double> doubles = new ArrayList<>();
    doubles.add(0.1);
    doubles.add(4.1);
    double wat = calculateAll(doubles);
    System.out.println(wat);
}

So lets take a note of all the changes we have done.

List<double[]> has been replaced with List<Double>.

List now holds items of instance Double

for(int i = 0; i < allNumbers.size(); i++) was changed to for (Double allNumber : allNumbers) this is just a basic for-each loop.

Honestly I would just use java 8 for this, We can do this in 1 line!

public static double calculateAll(List<Double> allNumbers) {
    return allNumbers.stream().mapToDouble(e -> e / allNumbers.size()).sum();
}
SamHoque
  • 2,978
  • 2
  • 13
  • 43