java recursion problem
-
hi all i am trying to implments a java method in which given a string of digits as a parameter, prints all the combinations of letters that string of digits could represent. for combination of letter we are using mobile text pad 2 is for abc,3 is for cde 123 is entered then max length of words will be 3 adg, adh, adi, aeg, aeh, aei, afg, afh, afi, bdg, bdh, bdi, beg, beh, bei, bfg, bfh, bfi, cdg, cdh, cdi, ceg, ceh, cei, cfg, cfh, or cfi. The 1 can be a, b or c The 2 can be d, e or f the 3 can be g, h or i this method is to be implemted using recursion i m totally confused where to start of with can anyone help me with some algorithm of code ....
-
hi all i am trying to implments a java method in which given a string of digits as a parameter, prints all the combinations of letters that string of digits could represent. for combination of letter we are using mobile text pad 2 is for abc,3 is for cde 123 is entered then max length of words will be 3 adg, adh, adi, aeg, aeh, aei, afg, afh, afi, bdg, bdh, bdi, beg, beh, bei, bfg, bfh, bfi, cdg, cdh, cdi, ceg, ceh, cei, cfg, cfh, or cfi. The 1 can be a, b or c The 2 can be d, e or f the 3 can be g, h or i this method is to be implemted using recursion i m totally confused where to start of with can anyone help me with some algorithm of code ....
Please, check carefully the code, I'm a bit rusty with Java...
public class Main {
static String []_keys= {"abc", "def", "ghi", "jkl","mno","pqrs","tuv","wxyz"};
static char [] _out;
public Main() {}
static private void comb(int [] iKeys, int iIndex)
{
int i;
int iKey = iKeys[iIndex]-1;
for (i=0; i<_keys[iKey].length(); i++)
{
_out[iIndex] = _keys[iKey].charAt(i);
if ( iIndex == iKeys.length-1)
{
System.out.println(_out);
}
else
{
comb(iKeys, iIndex+1);
}
}
}public static void main(String[] args) {
_out = new char[100];
int [] iKeys = {1,2,3};// input, i.e. keys "123" in this example
comb(iKeys, 0);
}
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Please, check carefully the code, I'm a bit rusty with Java...
public class Main {
static String []_keys= {"abc", "def", "ghi", "jkl","mno","pqrs","tuv","wxyz"};
static char [] _out;
public Main() {}
static private void comb(int [] iKeys, int iIndex)
{
int i;
int iKey = iKeys[iIndex]-1;
for (i=0; i<_keys[iKey].length(); i++)
{
_out[iIndex] = _keys[iKey].charAt(i);
if ( iIndex == iKeys.length-1)
{
System.out.println(_out);
}
else
{
comb(iKeys, iIndex+1);
}
}
}public static void main(String[] args) {
_out = new char[100];
int [] iKeys = {1,2,3};// input, i.e. keys "123" in this example
comb(iKeys, 0);
}
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]yeah the code is not a problem but i m actually trying to input String of digits from user and the same String should be passed to the method in the above method we are passing array of integers... please help ....how do we get to change with that....
-
yeah the code is not a problem but i m actually trying to input String of digits from user and the same String should be passed to the method in the above method we are passing array of integers... please help ....how do we get to change with that....
Just add the method
StringToArray
and change a bitmain
, for instance:public static void StringToArray(String sKeys, int [] iKeys)
{
int i;
for (i=0; i<sKeys.length(); i++)
{
iKeys[i] = (int)(sKeys.charAt(i)-'0');
}
}
public static void main(String[] args) {
int [] iKeys;
String s = "123";
iKeys = new int[s.length()];
StringToArray(s, iKeys);
_out = new char[100];
comb(iKeys, 0);
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Just add the method
StringToArray
and change a bitmain
, for instance:public static void StringToArray(String sKeys, int [] iKeys)
{
int i;
for (i=0; i<sKeys.length(); i++)
{
iKeys[i] = (int)(sKeys.charAt(i)-'0');
}
}
public static void main(String[] args) {
int [] iKeys;
String s = "123";
iKeys = new int[s.length()];
StringToArray(s, iKeys);
_out = new char[100];
comb(iKeys, 0);
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]thx for help but my problem isnt solved yet... the method comb is required to have the parameter passed as a String not integer of arays thx in advance....
-
thx for help but my problem isnt solved yet... the method comb is required to have the parameter passed as a String not integer of arays thx in advance....
harshad bhatia wrote:
the method comb is required to have the parameter passed as a String not integer of arays
Why? Is it an assignment (i.e. homework?)? Anyway you can easily figure out the necesary modifications, I suppose :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
harshad bhatia wrote:
the method comb is required to have the parameter passed as a String not integer of arays
Why? Is it an assignment (i.e. homework?)? Anyway you can easily figure out the necesary modifications, I suppose :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]yeah it is part of assignment its is like where i m struck in makin the method plz if u can help me out....
-
yeah it is part of assignment its is like where i m struck in makin the method plz if u can help me out....
i think i could do it now really thx for ur help......