How to use conditional clause on a field in Jasper report to get values?

I am working on a jasper report in that I am using a field "RACE_CODE" which contains values like 'abc','lmn','pqr','xyz'. For one person there can be multiple RACE_CODE assigned to it.
So I want the output in term of four digits like:
If a person have RACE_CODE 'abc' and 'xyz' then it should display '1001' and so on.
I can use conditional statement like:
    $F{RACE_CODE} == 'abc' && $F{RACE_CODE} == 'xyz' ? 
    '1001' : ($F{RACE_CODE} == 'abc' ? '1000' : so on
But this is not a feasible way because for four digits I will be having 12 combinations.
Is there any other way in which I can achieve this? Please help to solve this.
daad.madhusudan's picture
Joined: Mar 13 2015 - 4:36am
Last seen: 4 years 6 months ago

$F{RACE_CODE} == 'abc' && $F{RACE_CODE} == 'xyz' always false. 
>>For one person there can be multiple RACE_CODE assigned to it.
Report field have only one value.

Question is unclear

sanbez - 5 years 8 months ago

Thanks @sanbez for your response :).

Yeah you are right. One person can have multiple race codes. In my case there are 7 race codes and one person can have many race codes from these seven values. So for this, I want seven digit binary representation. 

Want i want is:


then display 1100000


display 1000001

I am taking some test values here like abc,lmn,xyz for demo.




daad.madhusudan - 5 years 8 months ago

1 Answer:


Why 7 digids? You start only with 4 :) I guess that one of the possible values of $F{RACE_CODE} is "abcxyz" (you don't write example of data). Is it right? In this case you can use expression like this (java code example):

  public static void main(String[] args) {
    String RACE_CODE = "abcxyz";
    System.out.println(RACE_CODE.indexOf("abc") == -1 ? "0" : "1" + (RACE_CODE.indexOf("lmn") == -1 ? "0" : "1") + (RACE_CODE.indexOf("pqr") == -1 ? "0" : "1") + (RACE_CODE.indexOf("xyz") == -1 ? "0" : "1"));   

sanbez's picture
Joined: Jan 11 2011 - 2:06am
Last seen: 4 months 3 weeks ago