I'm filling parameters in JasperSoft. In my report I have the Parameters: Parameter_1, Parameter_2, Parameter_3
int a;
for (a = 0; a < headers.length; a++) {
parameters.put("Parameter_" + a, headers[a]);
}
I was populating the Parameters in this fashion and it works. Now I want to add a new Parameter, Parameter_GroupBy which is determined by its index (let's say I want Parameter_2 to be the Parameter_GroupBy) so I did this:
int a;
for (a = 0; a < headers.length; a++) {
if (a == groupBy) {
parameters.put("Parameter_GroupBy", headers[groupBy]);
continue;
}
parameters.put("Parameter_" + a, headers[a]);
}
The problem with this code (assuming groupBy value is 2) is that Parameter_2 is blank but I want it to have the content of what Parameter_3
For example
Parameter_1= name
Parameter_2= date
Parameter_3= street
What I get with the second code bit
Parameter_1 = name
Parameter_2=
Parameter_GroupBy= date
Parameter_3= street
I want to group by date (Parameter_2) so I want
Parameter_1 = name
Parameter_2= street
Parameter_GroupBy= date
Parameter_3=
How can this be achieved? Using JDK 1.6 and Windows.
It seems to me that you just need to keep a separate index for the "next parameter to put":
int parameterIndex = 1;
// Note: more idiomatic to declare the iteration variable
// inside the loop
for (int headerIndex = 0; headerIndex < headers.length; headerIndex++) {
String header = headers[headerIndex];
if (headerIndex == groupBy) {
parameters.put("Parameter_GroupBy", header);
} else {
parameters.put("Parameter_" + parameterIndex, header);
parameterIndex++;
}
}
See more on this question at Stackoverflow