Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. java recursion problem

java recursion problem

Scheduled Pinned Locked Moved Java
helpjavaalgorithms
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    harshad bhatia
    wrote on last edited by
    #1

    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 ....

    CPalliniC 1 Reply Last reply
    0
    • H harshad bhatia

      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 ....

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      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]

      In testa che avete, signor di Ceprano?

      H 1 Reply Last reply
      0
      • CPalliniC CPallini

        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]

        H Offline
        H Offline
        harshad bhatia
        wrote on last edited by
        #3

        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....

        CPalliniC 1 Reply Last reply
        0
        • H harshad bhatia

          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....

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Just add the method StringToArray and change a bit main, 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]

          In testa che avete, signor di Ceprano?

          H 1 Reply Last reply
          0
          • CPalliniC CPallini

            Just add the method StringToArray and change a bit main, 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]

            H Offline
            H Offline
            harshad bhatia
            wrote on last edited by
            #5

            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....

            CPalliniC 1 Reply Last reply
            0
            • H harshad bhatia

              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....

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              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]

              In testa che avete, signor di Ceprano?

              H 1 Reply Last reply
              0
              • CPalliniC CPallini

                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]

                H Offline
                H Offline
                harshad bhatia
                wrote on last edited by
                #7

                yeah it is part of assignment its is like where i m struck in makin the method plz if u can help me out....

                H 1 Reply Last reply
                0
                • H harshad bhatia

                  yeah it is part of assignment its is like where i m struck in makin the method plz if u can help me out....

                  H Offline
                  H Offline
                  harshad bhatia
                  wrote on last edited by
                  #8

                  i think i could do it now really thx for ur help......

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups