Since the top part is mostly cookie-cutter, let's focus on the method evenPositionsOnly
. We need to think about the logical steps to accomplish this task, "print the values found in the array where the index is even".
We need to have something to hold our found values. Let's call it evenIndexedValues
:
int[] evenIndexedValues = new int[some size value here];
We need to know what size to make our new array. We know it's not going to be more than half the size of the original. We can add one just to make sure we have enough space, to keep this simple:
int size = (nums.length / 2) + 1;
int[] evenIndexedValues = new int[size];
Keep in mind that an int
divided an int
will only ever yield an int
, with the "reminder" being truncated. So, if your array size is an odd number, (nums.length / 2)
will yield a value that is less than the "real" half size; that's why we add one (although it's not actually necessary in this case, since our index won't reach that value).
Now that we have our container, we can loop through our nums
array, grabbing the value found at each even index and inserting it into our new array:
int size = (nums.length / 2) + 1;
int[] evenIndexedValues = new int[size];
for(int index = 0; i < nums.length; i += 2){
}
But wait! How do we know where to put our even value? We can't use i
, because that will have values like 0, 2, 4, etc. which would skip spots in our new array (not to mention go out of bounds pretty quickly). So, we need to define a local variable, and manually increment it by one each time through the loop:
int size = (nums.length / 2) + 1;
int[] evenIndexedValues = new int[size];
int j = 0;
for(int index = 0; i < nums.length; i += 2){
// some code here
j += 1;
}
So, the last bit is to add the code to insert our values, and then wrap this all in our method body:
public static int[] evenPositionsOnly(int[] nums){
int size = (nums.length / 2) + 1;
int[] evenIndexedValues = new int[size];
int j = 0;
for(int i = 0; i < nums.length; i += 2){
evenIndexedValues[j] = nums[i];
j += 1;
}
return evenIndexedValues;
}
Now that evenPositionsOnly
is defined, let's think about what main
should be doing. You start off mostly right:
Scanner input = new Scanner(System.in);
System.out.println("Please enter a array of numbers");
int n = input.nextInt();
int[] nums = new int[n];
The manner in which you use n
suggests that your intention was to ask for how many numbers to enter. Assuming that's correct, let's correct the prompt:
Scanner input = new Scanner(System.in);
System.out.println("Please enter the size of the array to create");
int n = input.nextInt();
int[] nums = new int[n];
Once we have that, we need to either prompt for those values, or generate them randomly. I assume you're supposed to prompt, so:
Scanner input = new Scanner(System.in);
System.out.println("Please enter the size of the array to create");
int n = input.nextInt();
int[] nums = new int[n];
for(int i = 0; i < n; i++){
System.out.printf("Value #%i?: ", i+1);
int value = input.nextInt();
nums[i] = value;
}
Then, add the last two lines from your example, wrap it up in main
, and you're pretty much done:
import java.util.Scanner;
import java.util.Arrays;
public class Trial7 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Please enter the size of the array to create");
int n = input.nextInt();
int[] nums = new int[n];
for(int i = 0; i < n; i++){
System.out.printf("Value #%i?: ", i+1);
int value = input.nextInt();
nums[i] = value;
}
int[] evens = evenPositionsOnly(nums);
System.out.println(Arrays.toString(evens));
}
public static int[] evenPositionsOnly(int[] nums){
int size = (nums.length / 2) + 1;
int[] evenIndexedValues = new int[size];
int j = 0;
for(int i = 0; i < nums.length; i += 2){
evenIndexedValues[j] = nums[i];
j += 1;
}
return evenIndexedValues;
}
}
Arrays.print is found in the same java.utils.*
namespace as Scanner - it's up to you whether to use that or loop through the array and print each value manually. See What's the simplest way to print a Java array? for more suggestions.