javabooleanmessageformat

How do I use a Java MessageFormat choice with a boolean?


Assume I want to say "Your order qualifies for a discount" or "Your order does not qualify for a discount".

I assumed I could use a choice with a boolean value in a MessageFormat, such as

"Your order {0,choice,false#does not qualify|true#qualifies} for a discount"

But I get the error

Choice Pattern incorrect: false#does not qualify|true#qualifies

Of course, there are many other ways I could achieve this, but does a MessageFormat choice not support boolean values?


Solution

  • ChoiceFormat does not work with boolean unfortunately, as others have mentioned. It seems there was at least one old JDK enhancement request to support this functionality, but it was rejected:

    https://bugs.openjdk.java.net/browse/JDK-4682566

    As such, you can use workarounds such as converting the boolean to a number first.