What's wrong with this code:
void bark(boolean hamlet) {
hamlet ? System.out.println("To Bark.") : System.out.println("Not to Bark");
}
What's wrong with this code:
void bark(boolean hamlet) {
hamlet ? System.out.println("To Bark.") : System.out.println("Not to Bark");
}
According to §JLS.15.25:
You can read why in the Java Language Specification, 15.25. Conditional Operator ? :
You need to do as several of the other answers suggest, and apply the conditional operator to just the argument.
I should imagine its because the ternary operator is expecting to assign a value. Try this:
Ternary operators must return something. So you can put it inside the print statement like such:
Or:
Ternary operators can't have statements that don't return values,
void
methods. You need statements that have return values.You need to rewrite it.
A ternary statement has to return something, you could use an if here: