title: Recursive string decompression
body: <p>I'm trying to decompress strings that looks as follows:</p>
  
  <p>Input: 4(ab)</p>
  
  <p>Output: abababab </p>
  
  <p>Input: 11ab</p>
  
  <p>Output: aaaaaaaaaaab</p>
  
  <p>Input: 2(3b3(ab)) </p>
  
  <p>Output: bbbabababbbbababab</p>
  
  <p>The above examples all comes out correctly using the recursive method below but problems arise when I input something like:</p>
  
  <p>Input: 4(ab)a</p>
  
  <p>Expected output: ababababa</p>
  
  <p>Input: 2(3b3(ab))a</p>
  
  <p>Expected output:bbbabababbbbabababa</p>
  
  <p>I realise the problems arise where in the return statement "return repeated". In its current state the recursion continues until it hits the end of the input string even after an ending parenthesis. Basically I don't know how to get it to break if reaching the ending parenthesis and then continue if there's anything left. In 2(3b3(ab))a it should return 2*(3b3(ab))+a, and now it returns 2*(3b3(ab))a. Any help is greatly appreciated, since I can't get my head around it.</p>
  
  <pre><code>public static String decompress(String compressedText) throws Exception
  {
     //BASE CASE 
      if(compressedText.length() == 1)
      {
          if(compressedText.charAt(0) == ')')
          {
              System.out.println("1: " + compressedText);
              return "";
          }
          else
          {
              System.out.println("2: " + compressedText);
              return compressedText;
          }
  
      }
      //END BASECASE
  
  
      if(compressedText.charAt(0) == '(')
      {
          System.out.println("3: " + compressedText);
          return decompress(compressedText.substring(1));        
      }
  
  
      //IF DOUBLE DIGIT
      if(Character.isDigit(compressedText.charAt(0)) == true &amp;&amp; Character.isDigit(compressedText.charAt(1)) == true)
      {
          if(compressedText.charAt(3) != '(')
          {
              System.out.println("4: " + compressedText);
              int i = Integer.parseInt(compressedText.substring(0,2));
              String repeated = new String(new char[i]).replace("\0", compressedText.substring(2,3));  
              return repeated + decompress(compressedText.substring(3));
          }
          else
          {
              System.out.println("5: " + compressedText);
              int i = Integer.parseInt(compressedText.substring(0,2));
              String repeated = new String(new char[i]).replace("\0", decompress(compressedText.substring(2)));
              return repeated;
          }
  
      }
      //END DOUBLE DIGIT
  
  
  
      //IF SINGLE DIGIT
      if (Character.isDigit(compressedText.charAt(0)) == true)
      {
          if(compressedText.charAt(1) !='(')
          {
              System.out.println("6: " + compressedText);
              int i = Integer.parseInt(compressedText.substring(0,1));
              String repeated = new String(new char[i]).replace("\0", compressedText.substring(1,2));  
              return repeated + decompress(compressedText.substring(2)); 
          }
          else
          {
              System.out.println("7: " + compressedText);
              int i = Integer.parseInt(compressedText.substring(0,1));
              String repeated = new String(new char[i]).replace("\0", decompress(compressedText.substring(1)));
              return repeated;
          }
  
      }
      //END SINGLE DIGIT
  
      //IF RIGHT PARENTHESIS
      if (compressedText.charAt(0) == ')')
      {
          if (compressedText.charAt(1) != ')')
          {
              System.out.println("8: " + compressedText);
              return "";
          }
          else
          {
              System.out.println("9: " + compressedText);
              return  decompress(compressedText.substring(1));
  
          }
  
      }
      //END 
  
          System.out.println("10: " + compressedText);
          return compressedText.charAt(0)+decompress(compressedText.substring(1));
  
  }
  </code></pre>