Help with using Reduce in Java

So I’m doing an exercise trying to find how many numbers in an array have even digits. Here is my solution in JavaScript

/**
 * @param {number[]} nums
 * @return {number}
 */
var findNumbers = function(nums) {
    ans = nums.reduce((evenCount,val) => {
       return ((Array.from(String(val)).length) % 2 === 0)? ++evenCount: evenCount;
    },0);
    return ans;
};

That seems to run fine. But then I try to do the same thing in Java

class Solution {
    public int findNumbers(int[] nums) {
        int ans = nums.stream().reduce(
        (numEven, current) -> {
          return  (String(current).length()%2 == 0) ?
              ++numEven  :  numEven;
        },0);
            return ans;
    }
}

I’m getting a "can’t find symbol reduce " error. What am I doing wrong? My goal here is to understand how to use reduce in Java not have a better algorithm for calculating even digits.

I don’t know Java but it looks like the initial value (they call it identity value) is the first argument and you likely need some sort of import as well, you can try import java.util.*;.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.