I am new to programming, but I have a need to group by elements, if they are selected by user; therefore, I am not exactly sure how many elements my group by will contain. Therefore I did this to count amount of selected elements and then, created another statement that creates correct Group by statement that represents the amount of elements needed.
It works, but it looks bulky and I would like to see if there are better ways of doing this?
int totalOutPuts = 0;
totalOutPuts = endbox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = coursebox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = departmentbox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = callbox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = daybox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = startbox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = instructorbox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = roombox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = buildingbox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = numberenrolled == true ? totalOutPuts + 1 : totalOutPuts;
int missingElements = 10 - totalOutPuts;
String groupBy = " Group by 1, 2, 3, 4, 5, 6, 7, 8, 9, 10";
if (missingElements == 9) {
groupBy = " Group by 1";
} else if (missingElements == 8) {
groupBy = " Group by 1, 2";
} else if (missingElements == 7) {
groupBy = " Group by 1, 2, 3";
} else if (missingElements == 6) {
groupBy = " Group by 1, 2, 3, 4";
} else if (missingElements == 5) {
groupBy = " Group by 1, 2, 3, 4, 5";
} else if (missingElements == 4) {
groupBy = " Group by 1, 2, 3, 4, 5, 6";
} else if (missingElements == 3) {
groupBy = " Group by 1, 2, 3, 4, 5, 6, 7";
} else if (missingElements == 2) {
groupBy = " Group by 1, 2, 3, 4, 5, 6, 7, 8";
} else if (missingElements == 1) {
groupBy = " Group by 1, 2, 3, 4, 5, 6, 7, 8, 9";
}