1

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++;
                }

Complete code

वरुण
  • 1,237
  • 3
  • 18
  • 50
unknown
  • 461
  • 9
  • 23
  • You may need to add check if `j` is less than length of `current` in this loop `while ((int) current.charAt(j) > 47 && (int) current.charAt(j) < 58)` – वरुण Dec 26 '17 at 07:43
  • 1
    Tried it, still goes out of range. And you probably thought about checking if j – unknown Dec 26 '17 at 07:46
  • Where are you initializing `current`? – वरुण Dec 26 '17 at 07:47
  • Please take a look at the hyperlink. I am initializing it in my search method, I used separate methods for each calculation. – unknown Dec 26 '17 at 07:49
  • Because you are working on `current` string, a check on j should be there with length of `current`. Try to add check of `j < current.length()` in this loop `while ((int) current.charAt(j) > 47 && (int) current.charAt(j) < 58)` like this `while ( j 47 && (int) current.charAt(j) < 58)` – वरुण Dec 26 '17 at 07:57
  • I swear that i have tried this, I mean, I swear. Anyway thanks a ton. You can add it as reply so I can upvote. – unknown Dec 26 '17 at 08:02
  • I have also tried it. It is working. You need to add a check on `current` not on `r`. – वरुण Dec 26 '17 at 08:03
  • I meant, I thought I tried it before and it was not working.Now it is. Thank you – unknown Dec 26 '17 at 08:04
  • We can always see the stack trace in console which contains line number where the exception occurred and debug at which point this exception occurs. Debug the code line by line and we can resolve many issues. – वरुण Dec 26 '17 at 08:13

3 Answers3

1

current is being processed even after last number of string is processed so StringIndexOutOfBoundsException exception occurs. Because current string is being processed, a check on j should be there with length of current. Add check of j < current.length() in while loop like this while ( j<current.length() && (int) current.charAt(j) > 47 && (int) current.charAt(j) < 58).

वरुण
  • 1,237
  • 3
  • 18
  • 50
0

Use split method to get the tokens of this sentence.

For each token, starting from the index of ascci number of digits, till the length of token, get the numbers stored in a map as values and character part of it as keys.

Then process the values of map to get the power of 3 and then get it's key value and return the word accordingly.

Anand
  • 2,239
  • 4
  • 32
  • 48
0

while (j < current.length() && (int) current.charAt(j) > 47 && (int) current.charAt(j) < 58) { br += current.charAt(j); j++; }

I changed only this and don't see OutOfRange exception anymore. If I understand your task right, the code looks complex and you using String for calculating, which is not efficient. Take a look at this approach:

public static void main(String[] args) {
    String v = "Iam8 you64 asjkj125 asdjkj333 heis216s";
    int sum = 0, max = 0, start = 0;
    StringBuilder maxWord = new StringBuilder();
    for (int i = 0; i < v.length(); ++i) {
        char ch = v.charAt(i);
        if (Character.isWhitespace(ch)) {
            max = updateMax(v, maxWord, max, sum, start, i);
            start = i + 1;
            sum = 0;
        } else if (Character.isDigit(ch)) {
            sum += ch - 48;
        }
    }
    if (sum > 0) {
        updateMax(v, maxWord, max, sum, start, v.length());
    }
    System.out.println(maxWord.toString());
}

private static int updateMax(String v, StringBuilder maxWord, int max, int sum, int start, int i) {
    if (sum >= max && isPowerOfThree(sum)) {
        maxWord.replace(0, maxWord.length(), v.substring(start, i));
        return sum;
    }
    return max;
}

private static boolean isPowerOfThree(int sum) {
    // see any from https://stackoverflow.com/questions/1804311/how-to-check-if-an-integer-is-a-power-of-3
    while (sum % 3 == 0) {
        sum /= 3;
    }
    return sum == 1;
}

Also, if you need the last appropriate word, then it would more efficient to start from the end of input string.

egorlitvinenko
  • 2,736
  • 2
  • 16
  • 36