The problem is you're not consistent with the meaning you give to count
inside your loops.
Possible meanings for count
1. count
represents the next item, the one that you want to return.
In this case your loops should start at count+1
and count-1
, respectively, and should both use fullString[i]
.
2. count
represents the current selected item, and you want the next.
In this case your loops should both start at count
and you should reference fullString[i+1]
(first loop) or fullString[i-1]
(second loop). You'll also have to be careful to change the end condition of your loops, so that i+1
and i-1
are not out of bounds.
Current problem: mixed approaches
In the current state of your code, you mix both approaches:
In the first loop, you start at
count
but usefullString[i]
. Making one press on the button useless because you don't increment count right away.In the second loop, you use the second option without changing the end condition. This will trigger an
ArrayIndexOutOfBoundException
whencount
is 0 (or you don't find a non-empty string before index 0), as @Mifeet pointed out in a comment you decided to ignore.
Improvement
Also, since you increment/decrement count
anyway, I would use it directly instead of adding an index i
. This makes your loops cleaner, and you can also get rid of the if
on count.
if (rotation) {
for (count++; count <= fullString.length - 1; count++) {
text = fullString[count];
if (text.trim().length() > 0) {
return text;
}
}
}
if (!rotation) {
for (count--; count >= 0; count--) {
text = fullString[count];
if (text.trim().length() > 0) {
return text;
}
}
}