title: Double a decimal string
body: <p>I am writing my own big integer class in java without imports and need a method for doubling a number of any size that is represented by a string. The code I have for this now works, but begins to take a long time once the numbers get bigger and bigger. I essentially create two arrays: the main array and the countdown array which both start as the same thing. Then, I run a while loop and increment the main array up and increment the countdown array down. When the countdown array reaches "0", I terminate the loop and the result is a new array with the new number doubled in size. Then of course I have if statements checking whether the arrays need to change the ten's place, etc.... here's what I have... Is there any way I can make it more efficient and quick?</p>
  
  <pre><code>public static String doubleDecimalString (String main) {
      String countdown = main;    
      String finalBuild = "";
      boolean runLoop = true;
  
      //if zero is supplied, skip all the nonsense and just return 0
      //else, loop through and find the true double
  
      //was having trobule getting single digits to double correctly so i had to hard code this for now.
     if (main.equals("0")) {
          return main;
      } else if (main.equals("5")) {
          return "10";
      } else if (main.equals("6")) {
          return "12";
      } else if (main.equals("7")) {
          return "14";
      } else if (main.equals("8")) {
          return "16";
      } else if (main.equals("9")) {
          return "18";
      } else {
          //Array for ORIGINAL NUMBER
          int[] mainPiece = new int[main.length()+2];
          int arrayLength = mainPiece.length;
  
          for ( int i = 0; i &lt; main.length(); i++ ) {
             mainPiece[i+2] = Integer.parseInt(main.substring( i, i+1));
          }
          mainPiece[0] = -1;
          mainPiece[1] = -1;
  
          //Array for COUNTDOWN NUMBER
          int[] countdownPiece = new int[main.length()+2];
  
          for ( int i = 0; i &lt; main.length(); i++ ) {
             countdownPiece[i+2] = Integer.parseInt(main.substring( i, i+1));
          }
          countdownPiece[0] = -1;
          countdownPiece[1] = -1;
  
          while (  runLoop ) {
  
              //Increment and decrement the two arrays
              mainPiece[arrayLength-1] += 1;    
              countdownPiece[arrayLength-1] -= 1;        
  
              //UPDATE MAIN ARRAY
              if (  mainPiece[arrayLength-1] == 10 ) {
                  for (int x = arrayLength-1; x &gt; 0; x--) {
  
                    if ( (mainPiece[x] == 10) &amp;&amp; (mainPiece[x-1] != 9) ) {
                          mainPiece[x] = 0;
                          mainPiece[x -1] += 1;
                      } else if ( (mainPiece[x] == 10) &amp;&amp; (mainPiece[x-1] == 9) ) {
                          mainPiece[x] = 0;
                          mainPiece[x -1] += 1;    
                          x = arrayLength;
                      } 
                       if ( (mainPiece[2] == 10) ) {
                          mainPiece[1] = 1;
                          mainPiece[2] = 0;
                      }
  
                  }
              } // end main array
  
            //UPDATE SIDE ARRAY
              if (  countdownPiece[arrayLength-1] == -1 ) {
                  for (int x = arrayLength-1; x &gt; 0; x--) {
  
                     if ( (countdownPiece[x] == -1) &amp;&amp; (countdownPiece[x-1] &gt; 0) &amp;&amp; (x &gt; 1)  ) {
                          countdownPiece[x] = 9;
                          countdownPiece[x -1] -= 1;
                      } else if ( (countdownPiece[x] == -1) &amp;&amp; (countdownPiece[x-1] == 0) &amp;&amp; (x &gt; 1) ) {
                          countdownPiece[x] = 9;
                          countdownPiece[x -1] -= 1;    
                          x = arrayLength;
                      }  
  
                  }
              } //end side array
  
  
             //tests whether the pieces need to be switched to -1 for scanning
              for (int x = 0; x &lt; arrayLength - 1; x++) {
                    if ( (countdownPiece[x] == -1 ) &amp;&amp; (countdownPiece[x+1] == 0 ) ) {
                          countdownPiece[x+1] = -1;
                    }
              }
  
              //if the side array has reached "0" then the loop will stop and the main array will return the new doubled value
              if ( (countdownPiece[arrayLength-1] == -1) &amp;&amp;  (countdownPiece[arrayLength-2] == -1) )   {
                  break;
              }
  
          } //end while loop
  
              //transform array into string
              finalBuild = "";      
              for (int T = 0; T &lt; arrayLength; T++) {
                  finalBuild += (mainPiece[T] != -1) ? mainPiece[T] : "";
               }
                return finalBuild;
      }           
  
  }
  </code></pre>