Android Text Function Forcecloses Rand Issue
Solution 1:
Hmm, I am uncertain about which lines are 186 and 188 (the two that throw the OutOfBounds exceptions). But looking at this loop, I have some suggestions:
for(int i2 = 0; i2 < length.get(trigger); i2++ ) {
From above we notice that length.get(trigger)
is always set to explode.size()
and we can see that explode.size()
never changes. We could check against explode.size()
instead or use a for-each loop again. Carrying on:
randint = rand.nextInt(length.get(trigger));
if(randint < length.get(trigger)/2) {
This looks like you want a 50/50 chance of: adding the original word to sentence
or
if(ary.get(i2)!=null) {
sentence.add(ary.get(i2));
}
}else{
sentence.add(words.get(rand.nextInt(words.size())));
}
}
... a random word to sentence
.
Also I beleive this could throw an exception:
if(ary.get(i2)!=null) {
sentence.add(ary.get(i2));
because you are checking the validity of i2
against length.get(trigger)
not ary.size()
. Now remember that ary
is simply a copy of explode
.
So let's condense this loop to:
for(String word : explode) {
if(rand.nextInt(1)) // 50/50 chance of being false or true (0 or 1)
sentence.add(word);
else
sentence.add(words.get(rand.nextInt(words.size())));
}
Take a look at the remaining two loops, if you apply a similar for-each loop you will be able to condense and expedite these as well. I don't know the rest of the app's code, but you might be able to remove a few Lists and Maps entirely. Cheers!
Post a Comment for "Android Text Function Forcecloses Rand Issue"