How to fix this error in java code


class Solution {
    public int trap(int[] height) {
        
        int left = 0, right = height.length  - 1;
    int ans = 0;
    int left_max = 0, right_max = 0;
    while (left < right) {
        if (height[left] < height[right]) {
            height[left] >= left_max ? (left_max = height[left]) : ans += (left_max - height[left]);
            ++left;
        }
        else {
            height[right] >= right_max ? (right_max = height[right]) : ans += (right_max - height[right]);
            --right;
        }
    }
    return ans;
    }
}

Hello @sudhakarmandaleeka00, welcome to the forums! What error is it throwing?

Is that even Java? Looks more like C. WDIK?

It looks like Java to me (although I’ve never used C…)

Just call me rabbit, I was looking for carats (pun intended).

You can tell I’ve never used either, hence the generic use of the name, ‘C’. It has so many flavors now.

1 Like

Here you basically say if LEFT_MAX == true DO height[left] >= left_max = height[left] ELSE height[left] >= ans += (left_max - height[left]).

From what I can understand is that you have your ternary operator wrong.

One correct use would be:

int num = 8;
String msg = "";
if(num > 10) {
    msg = "Number is greater than 10";
}
else {
    msg = "Number is less than or equal to 10";
}

TURNS TO

final String msg = num > 10 ? "Number is greater than 10"  : "Number is less than or equal to 10";

Okay, that I can believe is Java, sans the carats.