I seem to have encountered a tricky logic error in my code where the output is not as expected due to values not being returned correctly. I'm seeking guidance on how to troubleshoot this issue. Before delving into that, let me provide some context about what's happening. In the main section of my code, I am assigning values to an array 'x' which should then be passed to another file called getAllEvens. However, somewhere in the process, the values get altered or are not carried over properly, resulting in all zeros being displayed instead. Any suggestions or assistance would be greatly appreciated. Please aim to align your advice with the structure of my code or utilize simpler methods since this is for a school project. Let's proceed to the actual code.
Here is the code snippet from the main:
`int[] x = {2,4,6,8,10,12,14};
int[] x = {2,4,6,8,10,12,14};
int[] y = {1,2,3,4,5,6,7,8,9};
int[] z = {2,10,20,21,23,24,40,55,60,61};
System.out.println("Odds - " + Arrays.toString(OddsAndEvens.getAllOdds(x)));
System.out.println("Evens - " + Arrays.toString(OddsAndEvens.getAllEvens(x)));
And this is the corresponding section in the other file:
private static int[] cf = new int[20];
public static int countEm(int[] array, boolean odd)
{
cf=array;
return 0;
}
public static int[] getAllEvens(int[] array)
{
int[]vegeta = new int[20];
int VegetaScouterCount = 0;
int[] XboxThreeYearOld = new int[20];
for(int f : cf)
{
if(array[f]%2==0)
{
vegeta[VegetaScouterCount]=cf[f];
VegetaScouterCount++;
XboxThreeYearOld = Arrays.copyOfRange(vegeta, 0, VegetaScouterCount);
}
XboxThreeYearOld = Arrays.copyOfRange(vegeta, 0, VegetaScouterCount);
}
return XboxThreeYearOld;
}
(Ignore any informal language used)
The current output looks like:
Odds - []
Evens - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
The desired output should be:
Odds - []
Evens - [2, 4, 6, 8, 10, 12, 14]
I would really appreciate any pointers or hints in helping me resolve this issue.