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