Reputation: 461
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++;
}
Upvotes: 1
Views: 110
Reputation: 2776
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.
Upvotes: 0
Reputation: 1665
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)
.
Upvotes: 1
Reputation: 2359
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.
Upvotes: 0