0

For homework I am trying to print all possible combinations of letters that can be represented using the numeric keypad on a telephone. This problem is the same as the one posted here: How can I print out all possible letter combinations a given phone number can represent?

I keep getting a String index out of range: 0 problem but I'm not 100% sure why. Here is the code:

public class TelephoneKeyPad2 {
public static String keyPad[][] = {
        {"0"}, {"1"}, {"A", "B", "C"}, {"D", "E", "F"}, {"G", "H", "I"},
        {"J", "K", "L"}, {"M", "N", "O"}, {"P", "Q", "R", "S"},
        {"T", "U", "V"}, {"W", "X", "Y", "Z"}
};

public static void tpK(String num, StringBuilder combination) {
    if (num.length() == 0) {
        System.out.println(combination);
    }

    for (String letter: keyPad[Character.getNumericValue(num.charAt(0))]) {
        tpK(num.substring(1), combination.append(letter));
    }
}

public static void main(String[] args) {
    tpK("345", new StringBuilder());
} }

I have a feeling it's because of the if statement...there is no way to break the recursion after num.length == 0 is confirmed to be true, so the string index out of range. I am not sure how to fix this problem and any help would be appreciated.

EDIT: So it seems like it may be just a logic problem then afterwards.

DGJ

DGJK

DGJKL

DGJKLHJ

DGJKLHJK

are example outputs so now I'm really confused on how to solve this problem as well.

Community
  • 1
  • 1
Senescence
  • 63
  • 1
  • 7
  • "There is no way to break the recursion"--use a `return` statement or an else statement in order to allow the second half of the `tpK` method run *only* when `num.length()` isn't zero. – FThompson Feb 03 '15 at 05:22
  • Thanks for the responses...however it also seems like my logic is off as well. After adding the else statement in (or return statement), it returns: DGJ DGJK DGJKL DGJKLHJ DGJKLHJK DGJKLHJKL DGJKLHJKLIJ DGJKLHJKLIJK DGJKLHJKLIJKL DGJKLHJKLIJKLEGJ DGJKLHJKLIJKLEGJK DGJKLHJKLIJKLEGJKL DGJKLHJKLIJKLEGJKLHJ DGJKLHJKLIJKLEGJKLHJK DGJKLHJKLIJKLEGJKLHJKL – Senescence Feb 03 '15 at 05:31

3 Answers3

0

Add an else statement so that the for loop only gets executed when there is more than 0 characters:

public static void tpK(String num, StringBuilder combination) {
    if (num.length() == 0) {
        System.out.println(combination);
    } else {

        for (String letter: keyPad[Character.getNumericValue(num.charAt(0))]) {
            tpK(num.substring(1), combination.append(letter));
        }
    }
}
Jason
  • 11,744
  • 3
  • 42
  • 46
0

I solved the other problem I had. Turns it out the StringBuilder seems to create a static object(?). When the program goes through recursion, it only instantiates one object. I changed the StringBuilder type to String and used the concat method instead, solving the problem.

Senescence
  • 63
  • 1
  • 7
-1

If num.length() is 0, you still go on to ask for num.charAt(0).

John3136
  • 28,809
  • 4
  • 51
  • 69
  • While this is a true statement, it seems that the OP is asking about how to break the recursion in order to prevent exactly what you're pointing out. – FThompson Feb 03 '15 at 05:23
  • `I keep getting a String index out of range: 0 problem but I'm not 100% sure why` - this is why. Perhaps OP should be asking 2 different question ;-) – John3136 Feb 03 '15 at 05:24