oky_sabeni
oky_sabeni

Reputation: 7822

Ternary operator not working in Android

I have a simple question that boggles me. I am trying to use the ternary operator in java. I am new to Android and java. This code gives me the error:

amt < 0 ? lendBtn.setChecked(true) : lendBtn.setChecked(false);

"Syntax error on token "<", invalid AssignmentOperator"

So, I replace it with an if statement and it totally works:

if (amt < 0) { ... }

It's not a big deal but does anyone know why?

Upvotes: 3

Views: 3238

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500375

This has nothing to do with Android. You can't use a conditional expression as a statement on its own... and the second and third operands can't be void expressions either.

You should use:

lendBtn.setChecked(amt < 0);

... which is simpler to start with.

Upvotes: 16

Related Questions