Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.stream.IntStream;
  6.  
  7. /**
  8.  * Created by todor on 22.09.2017 г..
  9.  */
  10. public class P08_FirstOddOrEvenElements {
  11.     public static void main(String[] args) {
  12.         Scanner scan = new Scanner(System.in);
  13.        
  14.         int[] numbersArray = Arrays.stream(scan.nextLine().split("\\s+"))
  15.                 .mapToInt(Integer::parseInt)
  16.                 .toArray();
  17.         scan.next("\\w+");
  18.         int count = scan.nextInt();
  19.         String command = scan.next("\\w+");
  20.        
  21.         List<Integer> output = new ArrayList<>();
  22.         if (command.equals("even")) {
  23.             IntStream.of(numbersArray)
  24.                     .filter(i -> i % 2 == 0)
  25.                     .limit(count)
  26.                     .forEach(output::add);
  27.         }
  28.         if (command.equals("odd")) {
  29.             IntStream.of(numbersArray)
  30.                     .filter(i -> i % 2 != 0)
  31.                     .limit(count)
  32.                     .forEach(output::add);
  33.         }
  34.        
  35.         if (output.size() > 0) {
  36.             System.out.println(output.toString().replaceAll("[\\[\\],]", ""));
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement