Advertisement
Guest User

Untitled

a guest
Feb 29th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6. * Created by kaloy on 2/29/2016.
  7. */
  8. public class GetFirstOddOrEvenElements {
  9. public static void main(String[] args) {
  10. Scanner sc = new Scanner(System.in);
  11. String input = sc.nextLine();
  12. String command = sc.nextLine();
  13.  
  14. String[] commandSplitted = command.split("\\s+");
  15. int elementsNeeded = Integer.parseInt(commandSplitted[1]);
  16. String[] inputSplitted = input.split("\\s+");
  17. GetOddOrEven(inputSplitted,commandSplitted,elementsNeeded);
  18. }
  19. public static void GetOddOrEven(String[] inputSplitted,String[] commandSplitted,int elementsNeeded){
  20. for(int i =0;i<inputSplitted.length;i++){
  21. int currentNumber = Integer.parseInt(inputSplitted[i]);
  22. if(elementsNeeded<=0){
  23. break;
  24. }
  25. if(commandSplitted[2]=="odd"){
  26. if(currentNumber%2==1){
  27. System.out.printf("%d ",currentNumber);
  28. elementsNeeded--;
  29. }
  30. }
  31. else if(commandSplitted[2]=="even"){
  32. if(currentNumber%2==0){
  33. System.out.printf("%d ",currentNumber);
  34. elementsNeeded--;
  35. }
  36. }
  37. }
  38.  
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement