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?