Task is to check for numbers in each word of a sentence,find the word with the greatest number which is at the same time POWER OF 3. I did everything here and it works fine until my last word in the sentence ends with a number
For instance:
Input: Iam8 you64 asjkj125 asdjkj333 heis216 : OutOfBounds
Input: Iam8 you64 asjkj125 asdjkj333 heis216s : heis216s is target word
Then it will go out of bounds, I have already(at least I think I am) spotted where, but I have no idea how to avoid this without changing the idea of this calculation.
Here is the code where this occurs:
for (int i = 0; i < r.length(); i++) {
current+= r.charAt(i);
if (r.charAt(i) == ' ' || i = r.length()-1) {
for (int j = 0; j < current.length(); j++) {
while ((int) current.charAt(j) > 47 && (int) current.charAt(j) < 58) {
// If the last character of the last word is a digit, j increments and we end up out of bounds.
br = br + current.charAt(j);
j++;
}