Fixing comma logic in label expression

Dear Team,

I’m having an issue with an expression. It works greatly for another fields but for these one it always returns with a comma before “stationfieldnumber”, even when “method” and “collectingtripname” are null. I have no ideia what it is wrong.
“($F{1,10.collectingevent.method} == null?”“:$F{1,10.collectingevent.method})+($F{1,10,87.collectingtrip.collectingTripName} == null?”“:”, “+$F{1,10,87.collectingtrip.collectingTripName})+($F{1,10.collectingevent.stationFieldNumber} == null?”“:”, “+$F{1,10.collectingevent.stationFieldNumber})”.

Hi, please try this: remove the last comma, that is placed before the last row and add a comma after “collectingTripName” - like that: ($F{1,10,87.collectingtrip.collectingTripName} == null?“”:“, “+$F{1,10,87.collectingtrip.collectingTripName} + " , ")+($F{1,10.collectingevent.stationFieldNumber} == null?””: $F{1,10.collectingevent.stationFieldNumber})

Thank you, but it won’t works. When I have only the “method” and the “station field number” data (strange, but unfortunately it happens), the label returns without a comma between them.

Hi @juliannafb,

Thanks for your question (and thank you @VeselinGyonov for your help as well)! You can try this single expression that:

  • Adds the “method” value if it exists.
  • Then adds “, collectingTripName” only when the trip name is not empty and only prepending a comma if method was already added).
  • Finally adding “, stationFieldNumber” only when the station field number is not empty (and only prepending a comma if either of the earlier fields was added).

The result is a comma-separated string of whatever fields are present without any extra or missing commas:

(
  ($F{1,10.collectingevent.method}==null?"":$F{1,10.collectingevent.method})
  +($F{1,10,87.collectingtrip.collectingTripName}==null?"":(($F{1,10.collectingevent.method}==null?"":", ")+$F{1,10,87.collectingtrip.collectingTripName}))
  +($F{1,10.collectingevent.stationFieldNumber}==null?"":(((($F{1,10.collectingevent.method}!=null)||($F{1,10,87.collectingtrip.collectingTripName}!=null))?", ":"")+$F{1,10.collectingevent.stationFieldNumber}))
)

Thank you very much, @Grant !!! It’s working now!