-2

Given an integer n, using phone keypad find out all the possible strings that can be made using digits of input n. Return empty string for numbers 0 and 1.

Input

23

Output

ad
ae
af
bd
be
bf
cd
ce
cf

Code:

public class returnKeypad {
    private static String[] correspondingkepad(int n) {
        String keyWords[];

        switch (n) {
            case 2:  
                keyWords = new String[]{"a","b","c"};
                break;
            case 3:  
                keyWords = new String[]{"d","e","f"};
                break;
            case 4: 
                keyWords = new String[]{"g","h","i"};
                break;
            case 5:  
                keyWords = new String[]{"j","k","l"};
                break;
            case 6:
                keyWords = new String[]{"m","n","o"};
                break;
            case 7: 
                keyWords = new String[]{"p","q","r","s"};
                break;
            case 8: 
                keyWords = new String[]{"t","u","v"};
                break;
            case 9:  
                keyWords = new String[]{"w","x","y","z"};
                break;
           default:  
               keyWords = new String[]{""}; 
        }

        return keyWords;
    }

    public static String[] keypad(int n){
        if (n == 0) {
            String[] ans = {""};
            return ans;
        }

        String[] smallAns = keypad( n / 10 );      //line 45

        String[] p = correspondingkepad(n % 10);

        String[] ans = new String[p.length * smallAns.length];

        int k = 0;

        for(int i = 0; i < ans.length; i++) {
            for(int j = 0; j < p.length; j++) {
                ans[k] =  smallAns[i] + p[j];  //line 55             
                k++;
            }
        }

        return ans;
    }

    public static void main(String[] args) {
        String s = "xyz";
        String[] ans = keypad(234); //line 66

        for(int i = 0; i < ans.length; i++) {
            System.out.println(ans[i]);
        }
    }
}

Exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 
    at returnKeypad.keypad(returnKeypad.java:55)
    at returnKeypad.keypad(returnKeypad.java:45)
    at returnKeypad.keypad(returnKeypad.java:45)
    at returnKeypad.main(returnKeypad.java:66

I am mentioning lines Number in comments

Please tell me if i am wrong with this approach to the problem or what is wrong in my code Thank you

5
  • 1
    you are iterating over smallAns[i] till ans.length. These two are not be of the same size. Commented Jul 3, 2018 at 13:07
  • @gagansingh i dont understand how? i am new to community can you please elaborate? Commented Jul 3, 2018 at 13:10
  • 3
    it is not about community :) Learning to debug your code will be super useful for you. Commented Jul 3, 2018 at 13:12
  • 1
    it working now thanks a lot for helping @gagansingh Commented Jul 3, 2018 at 13:12
  • 1
    thanks @gagansingh for help i will learn to debug now thanks a lot for suggestion Commented Jul 3, 2018 at 13:14

2 Answers 2

2

you iterate on 'i' in the size of ans rather than smallAns

Change:

for(int i = 0; i<ans.length; i++) {
         for(int j =0; j<p.length; j++) {
             ans[k] =  smallAns[i] + p[j];  //Line55             
             k++;
         }
     }

To:

for(int i = 0; i<smallAns.length; i++) {
         for(int j =0; j<p.length; j++) {
             ans[k] =  smallAns[i] + p[j];  //Line55             
             k++;
         }
 }

That will fix the out-of-bound error, I'm not sure the rest of the logic makes sense...

Sign up to request clarification or add additional context in comments.

3 Comments

thanks its working now
@SarbjyotSinghChahal - then I would appreciate if you accept the answer (;
yes i will but this website ask me to wait for 5 min to accept the answer
1

Your smallAns[i] is not having any value other than ""and hence when i is getting increased to 1 it's failing with java.lang.ArrayIndexOutOfBoundsException. Please refine your logic in keypad(int n); Since you are using recursive call, on return it will come out of only last iteration.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.