'Simple int array logger method [duplicate]

Im making a method that is passed an array of positive and negative numbers. Im trying to return an array of the count of positive integers, aswell as the sum of the negative ones. Im fairly new to the ternary but im trying to implement one into this for practice. Getting an unexpected type error in my ternary. Was wondering if anyone could give some pointers

public static int[] countPositivesSumNegatives(int[] input)
{
  int[] answer = new int[2];
    for(int i=0; i<=input.length; i++){
      input[i] > 0 ? answer[0] += 1 : answer[1] += input[i];
    }
  return answer;
}


Solution 1:[1]

Ternarys return a value, not an expression. Your structure is more like a standard if-else.

The correct ternary structure looks like this:

value = condition ? true-value : false-value;

Since you're manipulating answer[1] on false and answer[0] on true, a ternary doesn't really make sense here. You'd need two of them to get both the index and the increment value. For the sake of answering your question though, here's an example:

int index = input[i] > 0 ? 0 : 1;
int value = input[i] > 0 ? 1 : input[i];
answer[index] += value;

At this rate, you might as well just use an if-else

if(input[i] > 0)
    answer[0] += 1;
else
    answer[1] += input[i];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Liftoff