The operator / is undefined for the argument type(s) float, Optional

I’m new in Java Language and I was trying to calculate a weighted average:

import java.util.ArrayList;
import java.util.Optional;
import java.util.Scanner;

public class App {
    public static void main(String[] args) throws Exception {
        Scanner keyboard = new Scanner(System.in);
        ArrayList<Float> grades = new ArrayList<Float>();
        ArrayList<Float> wheightes = new ArrayList<Float>();
        int count = 0;
        while(count < 3) {
          float grade = keyboard.nextFloat();
          grades.add(grade);
          count += 1;
        }
        System.out.println("Digite 1 para média ponderada e 2 para aritmética");
        int whichAverage = keyboard.nextInt();
        switch(whichAverage){
          case 1:
          float average = 0;
          for (int i = 0; i < grades.size(); i++) {
            average += grades.get(i);
          }
          System.out.println("A média aritmética é " + average / grades.size());
          case 2:
          System.out.println("Informe os 3 pesos:");
         float weightedAverage = 0;
          for (int i = 0; i < 3; i++) {
            float wheight = keyboard.nextFloat();
            wheightes.add(wheight);
            weightedAverage += grades.get(i) * wheightes.get(i);
          }
          Optional<Float> sumOfWheightes = wheightes.stream().reduce((prev, next) -> prev + next);
          System.out.println("A média ponderada é " + weightedAverage / sumOfWheightes);
          default:
          System.out.println("Digite 1 ou 2");
        }
        keyboard.close();
    }
}

I tried to do a reduce:

Optional<Float> sumOfWheightes = wheightes.stream().reduce((prev, next) -> prev + next);

Then I tried to divide a Optional<Float> with a float type:

System.out.println("A média ponderada é " + weightedAverage / sumOfWheightes);

The error is:

The operator / is undefined for the argument type(s) float, Optional<Float>

How can I do this division?

Answer

The error really says it all – you can’t divide a float by an Optioanl<Float>, you need to convert it to a float first. E.g., by using orElse:

float sumOfWheightes =
    wheightes.stream().reduce((prev, next) -> prev + next).orElse(1.0f);