I started learning Java 2 months ago and get stuck with this problem. Could anyone give me help on this one,please?
The Questions:
Given a string, for each digit in the original string, replaces the digit with that many occurrences of the character following.
So the string "a3tx2z" yields "attttxzzz".
My code:
@param str
@return blown up string
public static String blowup(String str) {
StringBuilder stri = new StringBuilder(str);
for(int i = 0; i<stri.length();i++){
if(Character.isDigit(stri.charAt(i))){
int a = stri.charAt(i),
c = a - 1;
char b = str.charAt(a+1);
while (a >0){
stri.insert(c, b);
a --;
}
}
}
str = stri.toString();
return str; // TODO ADD YOUR CODE HERE
}
The exception:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 52
at java.lang.String.charAt(String.java:658)
at HelloWorld.blowup(HelloWorld.java:15)
at HelloWorld.main(HelloWorld.java:6)