Not sure how to word this algorithm question, actually. I have toggle buttons in an android app, each corresponding to a numbered channel. Channels are (1,n) but the toggle button ids are some unknown, unsequential (to the buttons) integer. I need to collect the channel numbers and build a command string. My 1995-based Java skills have given me this:
String ch;
int i = 1;
for (ToggleButton toggle : toggles) {
if (toggle.isChecked()) {
ch = String.format("+%d", i+1);
channels = channels.concat(ch);
}
}
If toggle buttons 1,2,4,5,6,7,11,13,21,22,23,24,25 are checked, this code snippet successfully gives me the string "+1+2+4+5+6+7+11+13+21+22+23+24+25"
However, what I would more like to do is have the string "+1/2, +4/7, +11, +13, +21/25"
I wonder if there's an easier way to do this than multiple if statements:
String ch;
int it = 0;
int last = 1;
for (ToggleButton toggle : toggles ) {
if (toggle.isChecked()) {
if (it == last + 1) {
// somehow continue or note that we're only adding the "/"
} else {
// If we're not the next one, then build the last string
// of "n/last" and then add ", +" to start building next string
}
}
last++;
}
That seems like a bit of brute force type of algorithm, so I don't know if there's a more elegant solution that Java might have in it's bag of tricks (Or, more likely, that I should have)
Thanks.