-2

So i need to write a method that will find the even numbers of a passed array and return them back to the user. But, i am not returning them back as an array, but rather as ints. I am not sure why i am struggling with this. I am probably just overlooking something minor. I know how to get it to work if i was to return an array. any advice would be greatly appreciated. Here is the method.

 public static int numEven(int list[]){
    int evens = 0;
    System.out.print("The even numbers in your array are: ");
    for(int i = 0;i < list.length; i++ )
        if(list[i] % 2 == 0)
             evens = list[i];
    return evens;
}
prabodhprakash
  • 3,568
  • 22
  • 42
Doom3030
  • 15
  • 5
  • A function that's called once will only return a value once. It's not like you'll call a function one time and it will return values one after another multiple times. So if you want to return all the even values, then it's better to use an array or something from Collections or Maps – Raman Sahasi Sep 27 '16 at 03:34
  • make evens an array of list[] length and return the evens array? – Corgs Sep 27 '16 at 03:34
  • Possible duplicate of [How to declare an array](http://stackoverflow.com/questions/1200621/how-to-declare-an-array) – Shawn Mehan Sep 27 '16 at 03:34
  • Your code will return only one integer – Venkat Sep 27 '16 at 03:35
  • i would but i am not supposed to return it as an array for some reason. – Doom3030 Sep 27 '16 at 03:36
  • *"I know how to get it to work if i was to return an array"* Then...do that? What's the question? – T.J. Crowder Sep 27 '16 at 03:36
  • *"i would but i am not supposed to return it as an array for some reason"* Well, then how are you *supposed* to return it? Arrays aren't the only aggregate type. You could use a `List`, for instance. – T.J. Crowder Sep 27 '16 at 03:37
  • ill just just make it into an array because thats what makes the most sense to me – Doom3030 Sep 27 '16 at 03:39
  • I didn't understand why this is a difficult problem to solve. Simple logic: if(list[i] % 2 == 0) add to list or set and finally convert back to array. – Sri Sep 27 '16 at 03:49
  • As a side note, beware with modulo, see http://stackoverflow.com/questions/7342237/check-whether-number-is-even-or-odd –  Sep 27 '16 at 05:00

3 Answers3

2

Which is closer to the solution,

If you want to return the number of even,you can count up it like below.

// returns the number of "even"
    public static int numEven(int list[]) {
        int evens = 0;
        for (int i = 0; i < list.length; i++)
            if (list[i] % 2 == 0)
                evens++;

        return evens;
    }

Or if you want to return the evens in the input array,see below.

public static int[] numEvenAsAnArray(int list[]) {
        List<Integer> evens = new ArrayList<Integer>();

        for (int i = 0; i < list.length; i++) {
            if (list[i] % 2 == 0) {
                evens.add(list[i]);
            }
        }

        final int size = evens.size();
        int[] result = new int[size];
        for (int i = 0; i < size; i++) {
            result[i] = evens.get(i).intValue();
        }
        return result;

    }
riversun
  • 528
  • 5
  • 9
0

It's not really clear what you're wanting to do here. You're saying you don't want to return an array, but you want to return the even numbers back to the user? Are you trying to return a count of evens, or just the even numbers themselves. If the latter, than you DO need to return an int[] and you need to change your method signature to return an int[], not an int. Then you can leverage the Java 8 streams approach to do this in one simple line...

public static int[] numEven(int list[]){
    return IntStream.of(list).filter(i -> i % 2 == 0).toArray();
}

//Then here's a little helper main method where you really should put
//that output string you had in your utility method where you're returning
//your int[] from
public static void main(String[] args) {
    int[] ints = {1,2,3,5,7,8,9,10,12,13,14,15};
    int[] evens = numEven(ints);
     System.out.print("The even numbers in your array are: ");
    for(Integer i : evens) {
        System.out.println(i + ",");
    }

}
Bradley D
  • 1,819
  • 1
  • 11
  • 13
-1

This code works fine:-

public int countEvens(int[] nums) {
  int count=0;
  for(int i=0;i<=nums.length-1;i++){
    if(nums[i]%2==0){
      count++;
    }
    
  }
  return count;
}
ritesh_ratn
  • 159
  • 9
  • The same solution (just with the different variable names) is proposed in the [old answer](https://stackoverflow.com/a/39715657/6682517) for the same question. – Sergey Shubin Jun 25 '20 at 08:29
  • 1
    After reading your comment I noticed that a solution similar to mine is available here. I posted my answer just after reading the question. If I knew that a similar solution to mine is available here then I would not have posted my answer. – ritesh_ratn Jun 25 '20 at 08:42