Ternary operator with multiple condtions in Java throwing error [duplicate]

眉间皱痕 提交于 2019-12-11 10:43:31

问题


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?


回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/35330842/ternary-operator-with-multiple-condtions-in-java-throwing-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!