views:

98

answers:

2

I want to make a program that takes a set of numbers like 234, and at the moment, print out every combination of letters possible that are on a mobile phone keypad.

(1 - nothing, 2 - abc, 3- def and so on)

I currently have:

import java.util.*;

public class testCombo {
    static String str="217";
    static ArrayList<String> list=new ArrayList<String>();

    static void addLet(String seg){
        if(seg.length()==str.length()){
            list.add(seg);
            return;
        }
        char currentChar=str.charAt(seg.length());
        if(currentChar==1 || currentChar==0)
        {
            String str1=seg+" ";
            addLet(str1);
        }
        if(currentChar=='2'){
            addLet(seg+"a");
            addLet(seg+"b");
            addLet(seg+"c");
        }
        else if(currentChar=='3'){
            addLet(seg+"d");
            addLet(seg+"e");
            addLet(seg+"f");
        }   
        else if(currentChar=='4'){
            addLet(seg+"g");
            addLet(seg+"h");
            addLet(seg+"i");
        }   
        else if(currentChar=='5'){
            addLet(seg+"j");
            addLet(seg+"k");
            addLet(seg+"l");
        }   
        else if(currentChar=='6'){
            addLet(seg+"m");
            addLet(seg+"n");
            addLet(seg+"o");
        }   
        else if(currentChar=='7'){
            addLet(seg+"p");
            addLet(seg+"q");
            addLet(seg+"r");
            addLet(seg+"s");
        }   
        else if(currentChar=='8'){
            addLet(seg+"t");
            addLet(seg+"u");
            addLet(seg+"v");
        }   
        else if(currentChar=='9'){
            addLet(seg+"w");
            addLet(seg+"x");
            addLet(seg+"y");
            addLet(seg+"z");
        }   
    }

    public static void main(String[] args){
        addLet("");
        for(String str:list) //Sets str to each value in list during each iteration
            System.out.println(str);
    }
}

as my code as we are supposed to be using recursive programming, but I can't get it to work for 1s and 0s. (this is only a practice class, I have another one which allows input from user and it already verifies that it only contains digits)

Would this way of finding then printing out every combination count as recursive?

+1  A: 

Yes it's recursive (it works by calling itself), but it's unnecessarily verbose. You can skip the temporary variables, thus saving a lot of space, and making it more readable. It took me a few moments to grok why you had several string variables in each case:

    if(currentChar==1 || currentChar==0)
    {
        addLet(seg+" ");
    }
    if(currentChar=='2'){
        addLet(seg+"a");
        addLet(seg+"b");
        addLet(seg+"c");
    } ...

WRT 1's and 0's, you should be comparing currentChar to '1' and '0', not 1 and 0.

Marcelo Cantos
A: 

You can simplify the code and reduce chances of error by using a mapping of digits to candidate letters:

import java.util.*;
import static java.util.Arrays.asList;

public class TestCombo {

    private static Map<Character, List<Character>> lettersByDigit = 
        new HashMap<Character, List<Character>>() {{
           put('0', asList(' '));
           put('1', asList(' '));
           put('2', asList('a', 'b', 'c'));
           put('3', asList('d', 'e', 'f'));

           // and so on - add all candidates per digit in this fashion
        }};

    private static List<String> candidates = new Vector<String>();

    private static void enumerate(String digits, String prefix) {
        if (prefix.length() == digits.length()) {
          candidates.add(prefix);
          return;
        }

        char nextDigit = digits.charAt(prefix.length());

        for (Character nextLetter : lettersByDigit.get(nextDigit)) {
            enumerate(digits, prefix + nextLetter.toString());
        }
    }

    public static void main(String[] args){
        enumerate("217", "");
        for(String candidate : candidates) {
            System.out.println(candidate);
        }
    }
}

Note that this is not tested, but hopefully you get the idea.

oksayt