1

This is my ternary condition in Java.

new_user_id.equals(userid) && new_key.equals(key) && !new_value.equals(value) ? updateValue() : System.out.println("New value already exists in DB");

I'm trying to match three conditions and call updateValue() function if all three conditions gets true. But still its throwing error "The left-hand side of an assignment must be a variable". Any ideas?

Syed
  • 2,471
  • 10
  • 49
  • 89

2 Answers2

1

JLS-15.25. Conditional Operator ? : says (in part)

It is a compile-time error for either the second or the third operand expression to be an invocation of a void method.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

The ternary operator is used for conditional variable assignment or something like:

System.out.println("You have " + items + (items == 1 ? "item." : "items."));

Therefore, it must return something. Use an if statement instead.

Majora320
  • 1,321
  • 1
  • 13
  • 33