0

i am trying to find all possible decoding of a string of numbers, for example the following

Input: {1, 2, 1}
Output: ("aba", "au", "la") 
[3 interpretations: aba(1,2,1), au(1,21), la(12,1)]

My program pints "aba" then gives me the following error

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
at java.lang.String.charAt(String.java:686)
at countDecoding.decodings(countDecoding.java:20)
at countDecoding.decodings(countDecoding.java:17)
at countDecoding.decodings(countDecoding.java:17)
at countDecoding.main(countDecoding.java:37)

line 20 is      if((str.charAt(n)<'2' && str.charAt(n+1)!='0')  || (str.charAt(n)=='2' && str.charAt(n+1)<'6')){

and line 17 is          decodings(str,n=n+1,temp,len);

below is my code

public class countDecoding {
static void decodings(String str, int n,String temp,int len){

    if(n == len ){
         System.out.println(temp);
        return;
    }

    if(str.charAt(n)>='0'){
        char[] c = Character.toChars(96 + str.charAt(n)-'0');
        temp = temp +c[0];
        decodings(str,n=n+1,temp,len);
    }

    if((str.charAt(n)<'2' && str.charAt(n+1)!='0')  || (str.charAt(n)=='2' && str.charAt(n+1)<'6')){

        String hold = "";
        hold+=str.charAt(n);
        hold+=str.charAt(n+1);
        char[] c = Character.toChars(96 + (Integer.parseInt(hold)));
        temp = temp +c[0];
        System.out.println("temp is "+temp);
        decodings(str,n=n+2,temp,len);
    }

}

public static void main(String[]args){

    String str="121";
    decodings(str, 0,"",3);

}

}

Please help me find what is wrong with my implementation. I suck at recursion, so i am trying to horn my skills. Thanks

user3137376
  • 1,527
  • 2
  • 19
  • 29
  • Please tell us which line is line 20. – ajb Nov 04 '14 at 18:09
  • I suggest something like `System.out.println(n)` just before the `charAt` line that is causing the problem. It will probably help you see what errors there may be in your logic. Or use a debugger. – ajb Nov 04 '14 at 18:12

1 Answers1

0

The problem is the String goes from 0... n-1. Say length is 3 but the str.charAt(n) n should be from 0...2. You are calling it with 3 fix it. The line if referring to is :

if(str.charAt(n)<'2' && str.charAt(n+1)!='0')  || (str.charAt(n)=='2' && str.charAt(n+1)<'6'))

Even after fix that you will get errors cause you are doing n+1 also in that same line.


EDIT: Check the code now:

private void decodings(String str, int n,String temp,int len){

    if(n == len ){
        System.out.println(temp);
        return;
    }

    if(str.charAt(n)>='0'){
        char[] c = Character.toChars(96 + str.charAt(n)-'0');
        temp = temp +c[0];
        decodings(str,n+1,temp,len);
    }
    System.out.println("temp is "+temp);
}

public static void main(String[]args){
    String str="121";
    new countDecoding().decodings(str, 0,"",3);
}

OUTPUT IS :

aba
temp is aba
temp is ab
temp is a
StackFlowed
  • 6,664
  • 1
  • 29
  • 45
  • thanks, but i need those to check for (n) and (n+1). changing anything there will give me wring results, do you have any other ways i can implement this. – user3137376 Nov 04 '14 at 18:53
  • I could help you with he implementation if you give some sale input and sample output ! And explain what you want to do ! Also i don't understand what you mean by < in char comparison ? – StackFlowed Nov 04 '14 at 18:55
  • Input: {1, 1} Output: ("aa", 'k") [2 interpretations: aa(1, 1), k(11)] Input: {1, 2, 1} Output: ("aba", "au", "la") [3 interpretations: aba(1,2,1), au(1,21), la(12,1)] Input: {9, 1, 8} Output: {"iah", "ir"} [2 interpretations: iah(9,1,8), ir(9,18)] , i basically want to have the output as intepretations, in form of a string – user3137376 Nov 04 '14 at 18:58
  • One of the problem is you are using static method for recursion. In that you were doing n=n+2 you should not do that ... – StackFlowed Nov 04 '14 at 19:03
  • yh, but your output lacks "au" which is {1},{2,1}, and "la" which is {12},{1} – user3137376 Nov 04 '14 at 19:12
  • Yes now add that code the point was i removed static from your code and I changed n=n+2 in the parameter passing ! – StackFlowed Nov 04 '14 at 19:13
  • it gives Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3 at java.lang.String.charAt(String.java:686) at countDecoding.decodings(countDecoding.java:17) at countDecoding.decodings(countDecoding.java:13) at countDecoding.decodings(countDecoding.java:13) at countDecoding.main(countDecoding.java:32) – user3137376 Nov 04 '14 at 19:41