0

I need to convert an array into two separate arrays that odds and evens are separated into. I got this so far, but my output is not right. I would be very thankful if anybody could help me fix this, because it is due in class by the end of this weekend.

import java.util.Arrays;
import java.util.Scanner;

public class EvensAndOdds
{
private static int countEm(int[] array)
{
    int numOdd = 0; 
    for(int i=0; i< array.length; i++)
        if(array[i] % 2 == 1)  
            numOdd ++;

    return numOdd;
}
public static int[] getAllEvens(int[] array)
{
    int x = 0; 
    int[] evens  = new int[array.length - countEm(array)];
    for(int i=0; i<array.length; i++)
    {
        if(array[i]%2==0)
        {
            evens[x] = array[i];
            x ++; 
        }
    }
    return evens;
    }
public static int[] getAllOdds(int[] array)
{
    int y = 0;
    int[] odds = new int[countEm(array)];
    for (int i = 0; i < array.length; i++)
    {
        if (array[i] %2 == 1)
        {
        odds[y] = array[i];
        y++;
        }
    }
    return odds;
}

public static void main(String[] args)
{
    int[] input = {2,4,6,8,10,12,14};

    System.out.println(Arrays.toString(input));
    System.out.println("odds = " + EvensAndOdds.getAllOdds(input));
    System.out.println("evens =" + EvensAndOdds.getAllEvens(input));
}
}

The output comes out as:

[2, 4, 6, 8, 10, 12, 14]
odds = [I@659e0bfd
evens =[I@2a139a55
John C
  • 39
  • 6
  • 4
    System.out.println("odds = " + Arrays.toString(EvensAndOdds.getAllOdds(input))); – Andrew Williamson Jan 22 '16 at 17:46
  • It's currently printing the memory address of the array, and not the contents, because you aren't using Arrays.toString. You really should put an odd number in your input array, so you can tell if it's actually finding any odd numbers correctly. – Andrew Williamson Jan 22 '16 at 17:47
  • 2
    @AndrewWilliamson: It's not a memory address, but it's not useful info, either. :-) – T.J. Crowder Jan 22 '16 at 17:48

1 Answers1

0
Arrays.toString(EvensAndOdds.getAllOdds(input)))
munish
  • 453
  • 6
  • 22