0

I have an Jasper report that put my data into csv and I need something like this:

$F{OBSERVATII}.split(" ").size()>=2 ? ($F{OBSERVATII}.split(" ")[0]+" "+$F{OBSERVATII}.split(" ")[1]+" "+$F{OBSERVATII}.split(" ")[2]) : $F{OBSERVATII}

but Jasper throws me an

Cannot invoke size() on the array type String[]

If I put only

$F{OBSERVATII}.split(" ")[0]+" "+$F{OBSERVATII}.split(" ")[1]+" "+$F{OBSERVATII}.split(" ")[2])

then, when I have an simple word string (without spaces) it gives me an error...

Alex K
  • 22,315
  • 19
  • 108
  • 236
WDrgn
  • 521
  • 10
  • 29
  • [String.split](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29). [Java String array: is there a size of method?](http://stackoverflow.com/q/921384/876298) – Alex K Sep 10 '14 at 12:47
  • It's not about Java String array... the errors I get are from Jasper. In java I only need the .jasper file (from the .jrxml) and I'm not reaching the .jasper with those build errors... – WDrgn Sep 10 '14 at 14:23

1 Answers1

2

Final Edit:

The correct syntax for Jasper Reports is this. Evidently Jasper does not like using the property .length in this context.

 Arrays.asList($F{OBSERVATII}.split(" ")).size() ? blah...

Don't you want the length property for an array of strings, rather than the size method which does not work with an array?

.length >= 2 ? blah

Edited to say:

You said Jasper does not like the above syntax, but it's what you should use. You could try:

(String[] $F{OBSERVATII}.split(" ")).length>=2 ? blah...

Or

Arrays.asList($F{OBSERVATII}.split(" ")).size() ? blah...

That will convert your array to a list and use the size method. I don't know if Jasper will like that or not, if not, I suppose their expression editor sucks.

mikeb
  • 10,578
  • 7
  • 62
  • 120
  • I've tried .length but Jasper Expresion Editor gives me "The current expression is not valid. Please verify it!". I have to put $F{OBSERVATII}.split(" ").length()>=2 but that gives me "Cannot invoke length() on the array type String[]"... – WDrgn Sep 10 '14 at 14:20
  • @MihaiG That's because `length` is a property and not a method. Lose the parentheses and it should work (i.e. mikeb's code is exactly correct). – GenericJon Sep 10 '14 at 16:05
  • Arrays.asList($F{OBSERVATII}.split(" ")).size()>=2 works, (String[] $F{OBSERVATII}.split(" ")).length>=2 - don't, but it's ok, thanks! – WDrgn Sep 11 '14 at 06:51