deyanmalinov

04. Average of Doubles

Apr 21st, 2020
376
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package DPM;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class Main {
  7.     public static void main(String[] args) {
  8.         Scanner scan = new Scanner(System.in);
  9.        
  10.         List<Double> nums = Arrays.stream(scan.nextLine().split(" "))
  11.                 .filter(num -> !num.isEmpty())
  12.                 .map(Double::parseDouble)
  13.                 .collect(Collectors.toList());
  14.  
  15.         OptionalDouble aver = nums.stream()
  16.                 .mapToDouble(Double::valueOf)
  17.                 .average();
  18.  
  19.         if (aver.isPresent()){
  20.             System.out.printf("%.2f",aver.getAsDouble());
  21.         }else {
  22.             System.out.println("No match");
  23.         }
  24.     }
  25. }
RAW Paste Data