0

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)
Bakhtiar Hasan
  • 889
  • 10
  • 25
kai dai
  • 5
  • 4
  • 1
    In your code, you are not converting the character to a number. `int a = stri.charAt(i)` will return the character code, not the number. Try `int a = (stri.charAt(i) - '0')` – BretC Dec 06 '16 at 17:38

3 Answers3

0

As stated above, you are getting your stri's and str's muddled up and not converting numbers to ints...

public static String blowup(String str) {
    StringBuilder stri = new StringBuilder(str);
    for (int i = 0; i < str.length(); i++) {
        if (Character.isDigit(stri.charAt(i))) {
            int a = str.charAt(i) - '0',
                    c = a - 1;

            char b = str.charAt(i + 1);
            while (a > 0) {
                stri.insert(c, b);
                a--;
            }
        }
    }
    str = stri.toString();
    return str; // TODO ADD YOUR CODE HERE
}

(This still doesn't work correctly by the way, but it should help...)

BretC
  • 4,141
  • 13
  • 22
0

In short: when you try to get stri.charAt(i) as int it is not a numeric value - it is ASCII code of char. i.e. "ab3c".charAt(2) is not 3 but 51. Instead you need:

int a= Integer.valueOf(stri.substring(i,i+1));

BTW: are you sure that "a3tx2z" yields "attttxzzz" not to atttxzz" ?

Vadim
  • 4,027
  • 2
  • 10
  • 26
  • Thanks for help. I am sure that "a3tx2z" yields "attttxzzz" and another example, "2x3Z" yields "xxxZZZZ". – kai dai Dec 07 '16 at 03:00
  • np. it's just quite not logical: define 3t but got 4 tttt... but requirement is requirement :) – Vadim Dec 07 '16 at 11:19
0
public class Hello{
  public static void main(String ...args){
    String str = "a3tx2z";
    StringBuilder stri = new StringBuilder();
    for(int i = 0; i<str.length();i++){
        if(Character.isDigit(str.charAt(i))){
            int a = Character.getNumericValue(str.charAt(i));
            char b = str.charAt(i+1);
          if(a == 0){
            i++;
          }else{
          for(int j = 0;j<a-1;j++){
                stri.append(b);
            }
          }
        }else{
          stri.append(str.charAt(i));
        }
    }
    str = stri.toString();
    System.out.println(str);
  }
}

output :: atttxzz

Might this helps....

Akhil
  • 78
  • 12