-2
import java.util.Scanner; 

public class Trial7 { 
    public static void main(String[] args){ 
        Scanner input = new Scanner(System.in); 
        System.out.println("Please enter a array of numbers"); 
        int n = input.nextInt(); 
        int[] nums= new int[n]; 
        int[] nums2= evenPositionsOnly(nums); 
        System.out.println(nums2); 
    } 

    public static int[] evenPositionsOnly(int[] nums){ 
        //int[] result= new int[nums.length]; 
        for(int i = 0; i<nums.length; i=i+2) { 
            if(i/2==0) 
                nums[i]= nums[i] ; 
        } 

        return nums; 
    }
}

This is an update. I keep getting error, but am unsure why. Can anyone help with what the problem may be?

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
Amy
  • 1
  • 2
  • 1
    Think about what a return does. If you're not sure, find out. Then the answer is obvious. – Tieson T. Mar 05 '19 at 08:22
  • import java.util.Scanner; public class Trial7 { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.println("Please enter a array of numbers"); int n = input.nextInt(); int[] nums= new int[n]; int[] nums2= evenPositionsOnly(nums); System.out.println(nums2); } public static int[] evenPositionsOnly(int[] nums){ //int[] result= new int[nums.length]; for(int i = 0; i – Amy Mar 06 '19 at 05:45
  • If you're intending that as an update, use the edit button, which is found at the lower left of your question – Tieson T. Mar 06 '19 at 05:47
  • Thank you for that tip, sorry I am new to java so I am having a lot of trouble. when I change my iterator to i++, I get [I@55f96302 as the output. Would you happen to know why this happens? – Amy Mar 06 '19 at 06:03
  • 1
    Try stepping through your loop a second. Currently you iterate the entire array, and if the index is even, you set the value to what it is already equal to. This both leaves the array unchanged and unmodified entirely. You will need a new array (to insert into), as well as mapping indexes for it appropriately (using `/2` is still helpful there). – Rogue Mar 06 '19 at 06:05
  • @Rogue `i = i + 2` is the same as `i += 2` --> the value of `i` will only ever be even, given that it's starting at 0. That `if` is entirely unnecessary. – Tieson T. Mar 06 '19 at 06:09
  • `nums[i]= nums[i] ;` ?? – John3136 Mar 06 '19 at 06:13
  • @John3136 That line goes away as well. – Tieson T. Mar 06 '19 at 06:14
  • @TiesonT. Missed that bit of the loop and just assumed `i++` (wasn't too formatted) but the advice still stands that they need a new array and the indexes from old->new will not match 1:1. – Rogue Mar 06 '19 at 06:15
  • @Rogue Not sure what you're referring to. The code I added is from the comment above, formatted. – Tieson T. Mar 06 '19 at 06:16
  • You're correct, I'll edit that out. I checked the edit history and saw a comment spring up out of nowhere. – Rogue Mar 06 '19 at 06:17
  • I see that the if(i/2==0) is useless in my code, and that using i++ leaves to no longer have a error. However, I am struggling with the logic to make this code work. More specifically, how to return every number in the even position from my method. – Amy Mar 06 '19 at 06:23

1 Answers1

1

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.

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
  • Thank you! The logic does make sense to me, however my output is still turning out to be: [I@55f96302 . Do you know why that is? – Amy Mar 06 '19 at 06:40
  • Thank you once again! I really appreciate you thoroughly explaining all the code to me.However I am still getting a error. Do you know why that is? This code Is just for my own practice. What I am specifically struggling with is how to return a array from user input into the main method. I am practicing the logic of methods on coding bat. just how to return into a main method I do not understand. Thank you. – Amy Mar 06 '19 at 18:10