-4

I'm working on an assignment that requires the user to input an undecided number of integers, and then those numbers are to be used in other calculations. But for example if I have these 6 numbers. 1 2 3 4 5 6 and I want to extract only 2 4 and 6 from the input how would I do that? (make notices that this is just an example, the user can put in 20 different numbers, or only 4) I just want to be able to chose to pick every second element. To add more info, I need the first number in the array to be for one calculation, then the second for another calculation and then the first and the second. Basically I want to be able to assign every even array index numbers to a integer and every odd array index numbers to another integer. Like this :

"if (i % 2 == 0){int first = numbers [i]; System.out.println("This is the first" + first) } else if (i % 2 == 1) { int second = numbers [i] ; System.out.print("This is the second. " + second);}" But when I do so, it only appears in the first one..

7
  • Yes I know if I want the second index i have to take array[1] but if I want the 23rd array index and don't know that there will be a 23rd array index when I'm coding it. That is up for the user to put how many inputs there will be Commented May 4, 2017 at 1:37
  • Do you know how to write a for loop? Take a moment to think about what the starting index will be and how much you want to increment the index as you iterate over the array. Commented May 4, 2017 at 1:38
  • you need to collect all of elements that index is odd in an array due to array index start at 0. Commented May 4, 2017 at 1:40
  • Possible duplicate of Java Scanner Array Commented May 4, 2017 at 2:07
  • I solved something similar in this post Commented May 4, 2017 at 2:08

4 Answers 4

0

you need to collect all of elements that index is odd in an array due to array index start at 0.

// all of the second elements in numbers are collected into partial array
int[] partial = new int[numbers.length >> 1];
// the initial value of i=1 means iterate the first second element at first
// i+=2 means iterate the next second element in array
for(int i=1;i<numbers.length;i+=2){
  partial[(i-1)>>1] = numbers[i];
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you are familiar with Java 8 streams then you could use an IntStream to generate the indices and then pick out every odd one:

int[] everySecond = IntStream.range(0, array.length)
    .filter(i -> i % 2 == 1).mapToInt(i -> array[i]).toArray();

Comments

0

So just for clarification you want every second iteration of the array without the order of the numbers having any affect? So the array of numbers could be 4,2,6,8 and the return should be 2 and 8 in this example?

If this is the case then you can can assign the for loop to only read every second iteration.

To do this you will need the length of the array.

int arrLength = arr.length;
for (int i = 1; i < arr.length; i = i+2) {
#dosomething
}

6 Comments

It is also important to note that the total number of iterations of an array should always be n-1 where n is the length of the array. If the user inputs 10 numbers, then the total length of the array is 10. However, when reading through the array iteration by iteration - you will always begin the count at 0 ... hence in this example the max iteration will be 9. Also the reason why we use < instead of <= above when assigning the max iteration in the for loop
But then how do I use the numbers 4 and 6? because If I do a similar for loop for (int i = 0; i < arr.length; i = i++), then all the numbers in the array will be returned.
Made a quick edit above - the loop should begin at 1 not 0... To reference the actual array you can use the assigned variable i to reference to the correct index of the array... thus being able to use 4 and 6. In your example above 1 2 3 4 5 6 7 8... The first iteration will be i=1: you can reference array[i] to return 2 the next iteration will be i=3: you can reference array[i] to return 4 the next iteration will be i=5: you can reference array[i] to return 6 and so on....
if I do for (int i = 1; i < arr.length; i = i+2) { radius = arr[i * 2] (or just [i]) height = arr[i*2 +1] (or just [i+1] } I get out of bounds on the height, so the array index dont exist. Explain why?
Why are you performing radius = arr[i * 2]? If i = 5 and the array is only 8 in length for example - you'll be trying to access the 10th item in the list - which doesn't exist hence the out of bounds... to put this in basic terms if you have arr=[1,2,3,4,5] then arr[0]=1 arr[1]=2 arr[4]=5... where the length of the array is 5.
|
0
    // This is from user
    int[] userInput = {};

    int[] pickedNumbers = new int[userInput.length/2];
    if(userInput.length > 1)
    {
        for (int i = 1; i < userInput.length; i = i + 2)
        {
            pickedNumbers[i / 2] = userInput[i];
        }
    }
    // pickedNumber is  the array you can use.

2 Comments

Explain what this actually does? Because to me it just seams that you take the array pickedNumbers and put the even indexed ones in another array. And how does that help me?
Yes. That is what it exactly does. You can put your custom logic (what ever you want) with in the loop and enrich your pickedNumbers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.