Handle division is undefined on JasperReport - jasper-reports

I have variables in JasperReport 6.60 which is doing some weighted average, and I tried to avoid division by zero by passing it through IF condition but it still producing "Division is undefined"
Let's say I want to show a variable $V{weighted_avg} on a group called ([Group] round), which is consist of two rounds. On the 1st round, $V{divisor} is zero (0), but on the 2nd round $V{divisor} is not zero
The variable $V{weighted_avg} contains expression :
IF($V{divisor}.compareTo(BigDecimal(0.00)) == 1, $V{dividend}.divide($V{divisor}, new MathContext(4)), null)
it will produced error "Division is undefined" on that expression when calculated.
But if I am doing this :
IF($V{divisor}.compareTo(BigDecimal(0.00)) == 1, "> 0", "< 0")
the result is correctly showed.
All variables is Bigdecimal and I expected the $V{weighted_avg} is showing "null" when the $V{divisor} is zero

The builtin IF function evaluates all its arguments, you need to use the Java ternary operator so that only the expression that corresponds to the matching branch gets evaluated.
As in
$V{divisor}.compareTo(BigDecimal(0.00)) == 1 ? $V{dividend}.divide($V{divisor}, new MathContext(4)) : null

Related

How to use length and rlike using logical operator inside when clause

Want to check if the column has values that have certain length and contains only digits.
The problem is that the .rlike or .contains returns a Column type. Something like
.when(length(col("abc")) == 20 & col("abc").rlike(...), myValue)
won't work as col("abc").rlike(...) will return Column and unlike length(col("abc")) == 20 which returns Boolean (length() however also returns Column). How do I combine the two?
After doing a bit of searching in compiled code, found this
def when(condition : org.apache.spark.sql.Column, value : scala.Any) : org.apache.spark.sql.Column
Therefore the conditions in when must return Column type. length(col("abc")) == 20 was evaluating to Boolean.
Also, found this function with the following signature
def equalTo(other : scala.Any) : org.apache.spark.sql.Column
So, converted the whole expression to this
.when(length(col("abc")).equalTo(20) && col("abc").rlike(...), myValue)
Note that the logical operator is && and not &.
Edit/Update : #Histro's comment is correct.

getting the value from a checkbox in Matlab 2018

I am upgrading my Matlab from 2013b to 2018b and have found out that MathWorks have made quite a few changes to the GUI's.
One problem I am having is getting the value of checkbox. The line below is the code I used to use but now it doesn't work.
if get(handles.check_perf_attr,'Value') == 1
The error message is,
Undefined operator '==' for input arguments of type 'cell'.
So I tried the line below to just get the value that is being returned and then apply some logic.
tValue = get(handles.check_perf_attr,'Value');
However tValue is 2 x 1 cell which in (1, 1) = 0 & (2, 1) = 1. I don't really understand this as surely a checkbox can only be one value true (1) or false (0)?
get returns a cell array with values when applied to an array of handles.
Thus, I think your problem is that handles.check_perf_attr contains two handles, not one.
"Dot notation is a new syntax to access object properties starting in R2014b."
so try
if handles.check_perf_attr.Value == 1
or
tValue = handles.check_perf_attr.Value;

|| operators must be convertible to logical scalar values in case of an empty matrix

Consider the following code
t = ones(3,5)
Ind2save = find(t(1,:) == 0,1,'first')
So for example I am trying to find if even the first zero of the first row, so if the first element is a non zero then
if(Ind2save ~= 1 )
disp('no')
end
now for the above condition it doesn't display 'no' because the condition is not fulfilled but because all the rows are filled and Ind2save is an empty matrix so we another condition to check if it is fully filled then
if(Ind2save > 1 || isempty(Ind2save))
disp('no')
end
I get the following error
Operands to the || and && operators must be convertible to logical scalar values.
I searched for the reasons due to which this error is caused and in majority of the cases people were comparing two vectors so a better idea was to replace || with | but in my case the conditions are never vectors but Ind2save > 1 returns an empty matrix , does anyone know why is the reason for that? How can I accommodate both the conditions?
The issue is because in your case Ind2save is empty ([]) therefore the first part of your condition can't be used with || since [] > 1 doesn't yield a logical scalar (it results in []).
In order to fix this, you can to flip the order of your conditions such that you check if the array is empty first.
if isempty(Ind2save) || Ind2save > 1
The reason that this works is that if Ind2Save is empty, then the first condition evaluates to true therefore short-circuiting the rest of the checks.
You may have other issues if for some reason Ind2save is a vector. In that case you could need to so something to convert it to a logical scalar:
if isempty(Ind2save) || ismember(1, Ind2save)

Sum with conditions jasper studio

It is necessary to summarize the field $F{energy} wit conditions $F{type_energy} like "Active Energy".
I try with this(define Variable Sum) , with conditions in Variable Expression:
$F{type_energy} == ("Active Energy")? $F{energy}: new Double(0)
Boolean.valueOf($F{vrsta_energije}=="Active energy")?$F{iznos_naknade_km}.doubleValue():new Double(0)
new Double($F{vrsta_energije}!= null && $F{vrsta_energije}.equals("Active energy")?$F{kolicina_vrsna_snaga}.doubleValue():0)
IF($F{type_energy} == ("Active Energy"), $F{energy}, null)
where type of fild is:
$F{type_energy} java.lang.String
and $F{energy} java.math.BigDecimal
Keep getting highscore 0.0 or null
change data type of $F{energy} to double in feild expression.
Define a variable sum ($V{Sum}).
keep calculationFunction to none.
Initial value expression as 0.0.
In variable expression have
("Active Energy").equalEgnoreCase( $F{type_energy} ) ? $V{Sum}+$F{energy}: $V{Sum}

How to write condition on report count variable in jasper report

My problem is I want Report_Count variable to be null if it increases to 28. so for any value greater than 28 it will become null.
I have written this condition in print when expression property of Report Count variable
($V{REPORT_COUNT} > 28) ? $V{REPORT_COUNT} = null : $V{REPORT_COUNT}
Is it valid or it contains any errors? I don't know why it is not working.....
Any help would be appreciable.....
If you are referring to the built-in REPORT_COUNT variable, as far as I know, you cannot change its value. You could, however, define your own variable, for instance MY_REPORT_COUNT, and print it in your textfield.
For the variable class type - choose for example Integer.
For the variable expression:
$V{REPORT_COUNT}.intValue() > 28 ? null : $V{REPORT_COUNT}