Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Lekciq;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.function.Function;
- import java.util.stream.Collectors;
- public class SumNumbers {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Read input line
- String input = scanner.nextLine();
- // Define a function to parse strings to integers
- Function<String, Integer> parseInteger = Integer::parseInt;
- // Split the input into strings, parse them to integers, and collect into a list
- List<Integer> numbers = Arrays.stream(input.split(", "))
- .map(parseInteger)
- .collect(Collectors.toList());
- // Calculate the count and the sum of the numbers
- int count = numbers.size();
- int sum = numbers.stream().mapToInt(Integer::intValue).sum();
- // Print the count and the sum
- System.out.println("Count = " + count);
- System.out.println("Sum = " + sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement