[Solved] Java – ternary Operator not working. Compiler stating it’s not a statement [duplicate]

Ternary operator expressions are not statements: you can’t use them on their own as a replacement for an if. You should write if (unprocessed_information.contains(“type:”)) { ( unprocessed_information.replace(“type:”,””).equals(“teacher”) ) ? (admin_accounts.add(username)) : (null); } as: if (unprocessed_information.contains(“type:”)) { if (unprocessed_information.replace(“type:”,””).equals(“teacher”)) admin_accounts.add(username); } or (better, since there is nothing in the first if but the second if): … Read more

[Solved] use ternary operator to solve multiple conditions [closed]

Take a look here: class Question05 { public static void main(String[] args) { double gpa = Double.parseDouble(args[0]); String res = gpa >= 3.6?”First class Hons”:(gpa<3.6 && gpa>=3.4?”Upper Second Class Hons”: (gpa<3.4 && gpa>=3.0?”Lower Second Class Hons”: (gpa<3.0 && gpa>=2.0?”Pass”:”you have failed”))); System.out.println(res); } } Edit: @veena, you were trying to assign a string to gpa, … Read more

[Solved] Function overloaded by bool and enum type is not differentiated while called using multiple ternary operator in C++

That is because the ternary operator always evaluates to a single type. You can’t “return” different types with this operator. When the compiler encounters such an expression he tries to figure out whether he can reduce the whole thing to one type. If that’s not possible you get a compile error. In your case there … Read more

[Solved] Multiple ternary operators and evaluating int as boolean mystery [closed]

Why does any combination of true and false return the value it does? There is no combination of any boolean here. This ?: operator returns first or second expression, not the condition itself. condition ? first_expression : second_expression; The condition must evaluate to true or false. If condition is true, first_expression is evaluated and becomes … Read more