switch statement in a variable

1

 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;
 
}</td></tr></tbody></table>
rossi_jeff's picture
Joined: Nov 2 2010 - 8:27am
Last seen: 1 year 5 months ago

6 Answers:

0

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

cbarlow3's picture
1470
Joined: Mar 4 2010 - 8:59am
Last seen: 3 months 1 week ago
0
This worked but I'd still like to know why swictch did not.

("Integer,String,Float,Digital,Complex,Location,Diagnostics,Bitmap".split(",")[$F{sensor_readings_sensor_type}.intValue()])
rossi_jeff's picture
Joined: Nov 2 2010 - 8:27am
Last seen: 1 year 5 months ago
0

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

mdahlman's picture
11540
Joined: Mar 13 2007 - 2:43am
Last seen: 5 years 3 months ago
0

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.

 

 

noa123's picture
5
Joined: Jul 31 2011 - 2:57am
Last seen: 8 years 7 months ago
3
Don't forget to enclose in parentheses:

( $F{FIELDV}.equals("ab") ? "aaaa" :
( $F{FIELDV}.equals("bc") ? "bbbb" :
( $F{FIELDV}.equals("de") ? "cccc" : "" )))
despec's picture
154
Joined: Jan 26 2010 - 11:45am
Last seen: 6 years 6 months ago
0

It work's great!

thank's!!

noa123's picture
5
Joined: Jul 31 2011 - 2:57am
Last seen: 8 years 7 months ago
Feedback
randomness