Jump to content
We've recently updated our Privacy Statement, available here ×

switch statement in a variable


rossi_jeff

Recommended Posts

 Is there a way to make this work?

I have various values I need to show according to the value of an integer value in a field.

I've tried many different syntax but all end in compile errors.

 

Code:
switch ( $F{sensor_readings_sensor_type}.intValue() ) {    case 0: "Integer"; break;    case 1: "String"; break;    case 2: "Float"; break;    case 3: "Digital"; break;    case 4: "Complex"; break;    case 5: "Location"; break;    case 6: "Diagnostics"; break;    case 7: "Bitmap"; break;}
Link to comment
Share on other sites

  • Replies 6
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

I often just add a field to the query itself so it shows up as a field in iReport:

SELECT ...

sensor_readings_sensor_type,

CASE sensor_readings_sensor_type

   WHEN 0 THEN 'Integer'

   WHEN 1 THEN 'String'

   WHEN 2 THEN 'Float'

   WHEN 3 THEN 'Digital'

   WHEN 4 THEN 'Complex'

   WHEN 5 THEN 'Location'

   WHEN 6 THEN 'Diagnostics'

   ELSE 'Unknown'

END AS sensor_type_description

Link to comment
Share on other sites

You cannot have full lines of code. (If you see a semi-colon then you're probably going to have trouble.) You can only have expressions.

Therefore you could also solve switch type problem like this:

sensor_readings_sensor_type == 0
  ? 'Integer'
  : sensor_readings_sensor_type == 1
    ? 'String'
    : 
sensor_readings_sensor_type == 2
      ? 'Float'
      : and so on...

But frankly, your solution appears much nicer. (But I'm not sure whether yours would degrade nicely if the incoming value for sensor_readings_sensor_type is very large or otherwise unexpected.)

Regards,
Matt

Link to comment
Share on other sites

  • 5 months later...

Hi ,

What if my field value is a string?

I tried this:
$F{FIELDV}.equals("ab") ? "aaaa" :
$F{FIELDV}.equals("bc") ? "bbbb" :
$F{FIELDV}.equals("de") ? "cccc" : ""

but it didn't work... any idea?

Thank's In Advance.

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...