/*
Online Java - IDE, Code Editor, Compiler

Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

//I have declared all variables class level and static to ensure
//that the recursive method is kept tidy
//the question also raises performance question
//to ensure I can test the execution time, I will add stopwatch..
//For moment, I am using long variable to ensure I can extend 
//the String to 9,223,372,036,854,775,807

public class Main
{
    static int lengthFirstNumber;
    static int lengthSecondNumber;
    static String lastDigitFirstNumber;
    static String lastDigitSecondNumber;
    static int firstDigitTotalDigits;
    static int lastDigitTotalDigits;
    static int total;
    static String grandTotal="";
    static boolean remainder=false;
    static String remainingPortion;
    static int remainingPortionInteger;
    static String remainingPortionString;
    
    static int lastDigitFirstNumberRemainingPortion;
    static int lastDigitSecondNumberRemainingPortion;
    
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        System.out.println("Addition program, ensure String has maximum characters: " + "2,147,483,648");
        
        //Note this is a fixed version of previous code
        //but it required so much additional code to be added
        //it now functions formatted as below (without whitespace)
        //ALTHOUGH NO VALIDATION HAS BEEN PROVIDED, 
        //DO NOT INCLUDE WHITESPACES IN STRING WHATSOEVER
        //THIS REPLICATES THE CHALLENGE
        
        //String number2 =  "59";
        //String number1 = "652";
        
        //String number2 = " 544";
        //String number1 = "4666";
        
        //String number2 = "136";
        //String number1 =  "24";
        
        String number2 = "84636";
        String number1 =   "324";
        
        //For an unknown reason, the addition method is unable to return the String, it just shows blank value
        //or just skips the return statement
        //Bizarre is that it can see the variable value on screen before I try to return it.
        //So I have just used System.out.println(GRANDTOTAL).
        
        System.out.println(number1 + "+" + number2 + "= " + addition(number1, number2));
        
    }
    
    public static String addition(String number1, String number2)
    {
        //it is still working constraints of the code
        //the Strings are note being converted, it is only the length of the Strings.
        lengthFirstNumber=(number1.length());
        lengthSecondNumber=(number2.length());
        
        /*
        //at this point it knowws that its on the most extreme left hand side of them...
        //looking at example      4666
        //                      +  544
        //it is just not a case of dropping the number 4 down.... into the grandTotal
        //this would be ok if  (6+5 was not >=10)
        //there would be last addition undertaken
        //at this point its expected to perform
                                   4
                                +     
        
        
        //another example is just a case of dropping the remainder across
        //for instance performing  55
                                 + 55
                                 -------
                                  110
                                 -------
       //Another example is dealing with larger differences:
                                274666
                                +  544
                                
      //it would need to carry 1, add it to 4
      //it would then have to truncate number1 again and drop 2 and 7
      */
      
      //if both lengths are 0, it means both had same lengths.
      if (lengthFirstNumber==0 && lengthSecondNumber==0)
      {
            //this is a slightly ambiguous name, but it identifies if
            //total from digit in number1 number2 is one or two digits long
            //firstDigitTotalDigits = (int)total/10;   
            //if there is no frontal number (i.e) total less than 10, expect 0 in firstDigitTotalDigits
            //if total is between 10 -  18(9+9),   expect  firstDigitTotalDigits=1
            
            if (firstDigitTotalDigits==0)
            {
                System.out.println("GRANDTOTAL: " + grandTotal);
                return grandTotal;
            }
            
            else
            {
                //it would append a 1 to the left of existing most significant digit
                grandTotal = "1" + grandTotal;
                System.out.println("GRANDTOTAL: " + grandTotal);
                return grandTotal;
            }
            
      }
      //unfortunately variable names are extremely confusing
      //need to remember   it takes values from here onwards with one argument
      //if number1 was  9123   lengthFirstNumber=4
      //there is a digit at index 3,  so variable is reduced by 1
      
      //There will be issue here if the Strings are not padded so that it fills the same
      //length
      
      System.out.println("**********");
      //if((int)(number1/10)==0  || (int)(number/10)==0)
      //{
          
     // }
      
      System.out.println(number1);
      System.out.println(number2);
      
      
      
      
        
      //need something here since it was giving numberformat exception.
      //most likely since if no value in Strings (lastDigitFirstNumber
      //or lastDigitSecondNumber) BUT NOT BOTH. 
      // it can not get Integer value henceforth.
      //note, it would have already dealt with circumstance of processing all
      //digits in both uniform numbers above in first loop in addition method.
      
       try
       {
           lastDigitFirstNumber=number1.substring(lengthFirstNumber-1);
            lastDigitSecondNumber=number2.substring(lengthSecondNumber-1);
           //checks if the total is greater than 10. This is the same index on number1, number2 and also
           //carry forward 1 if applicable
           if ((Integer.valueOf(lastDigitFirstNumber) + Integer.valueOf(lastDigitSecondNumber) + firstDigitTotalDigits)>=10)
           {
               total = Integer.valueOf(lastDigitFirstNumber) + Integer.valueOf(lastDigitSecondNumber) + firstDigitTotalDigits;
               System.out.println("\nOVER 10");
               System.out.println("addition of: " + Integer.valueOf(lastDigitFirstNumber) + " + " + Integer.valueOf(lastDigitSecondNumber));
            
            System.out.println("this is current total: " + total);
            
            //first digit carried forward
            firstDigitTotalDigits = (int)total/10;
            System.out.println("This is first digit, it will be carried forward: " + firstDigitTotalDigits);
            
            remainder=true;
            
            //this is stored in the String
            lastDigitTotalDigits=total%10;
        
            //it has to be stored on the most left hand side like real life
            grandTotal = Integer.toString(lastDigitTotalDigits) + grandTotal;
          
            System.out.println("running grand total: " + grandTotal);
            System.out.println("length first: " + lengthFirstNumber);
             System.out.println("length second: " + lengthSecondNumber);
           
            //try
            //{
            addition(number1.substring(0, (lengthFirstNumber-1)), number2.substring(0, (lengthSecondNumber-1)));
            //}
            //catch (ArrayIndexOutOfBoundsException e)
            //{
              //  System.out.println("one of the numbers is shorter than other");
            //                    System.exit(0);
            //}
            
            //need to worry about carry forward in recursive call
        }
        else
        {
            //we expect total to be a single digit
            
            total = Integer.valueOf(lastDigitFirstNumber) + Integer.valueOf(lastDigitSecondNumber) + firstDigitTotalDigits;
            
            grandTotal = Integer.toString(total) + grandTotal;
            System.out.println("\nless than 10");
            System.out.println("addition of: " + Integer.valueOf(lastDigitFirstNumber) + " + " + Integer.valueOf(lastDigitSecondNumber));
            
            System.out.println("this is current total: " + total);
            
            System.out.println("running grandtotal: " + grandTotal);
            
            System.out.println("length first: " + lengthFirstNumber);
            System.out.println("length second: " + lengthSecondNumber);
            
           //it needs to set the carry forward value to 0
            firstDigitTotalDigits=0;
            
            //try
            //{
            //we now pass the exact starting number, but trim last number off
            addition(number1.substring(0, (lengthFirstNumber-1)), number2.substring(0, (lengthSecondNumber-1)));
            //}
            //catch (ArrayIndexOutOfBoundsException e)
            //{
                
            //}
            }
    }
    //ExceptionType1  Exceptiontype2 ex
    catch (NumberFormatException | StringIndexOutOfBoundsException s)
    {
        System.out.println("123This is last digit first number:" + lastDigitFirstNumber);
        System.out.println("123This is last digit second number:" + lastDigitSecondNumber);
        
        //now in here, it has to check if there is a carry over of 1
        //if so it would add it to last digit of either number1 or number2
        
        //for initial numbers such as, it is fortunately relatively more straight forward
           //4636
      //+     524
      
      //but it was in a try and catch since if any of the values:
      //lastDigitFirstNumber="" or lastDigitSecondNumber=""
      //it would cause NumberFormatException since it can not format String => int
      //if the String is empty! We know however ONLY one of them is empty
      //if ((Integer.valueOf(lastDigitFirstNumber) + Integer.valueOf(lastDigitSecondNumber)
      //the alternate technique would be to add 0's on the front of the shortest number
      //to ensure both have uniform length, however this would affect readability when performing
      //System.out.println()
      
      // this is fine here
      System.out.println("wwww****************");
      
       //String number2 = "136";
       //String number1 =   "24";
      
      
      System.out.println("number 1 at this point: " + number1);
      System.out.println("number 2 at this point: " + number2);
      
      
     //since it has issues processing lastDigitSecondNumber or 
     //lastDigitFirstNumber since both are truncated at same time in above code,
     //it has been forced here...
     //which is within the catch statement for 
     //(NumberFormatException | StringIndexOutOfBoundsException s)
     //for instance in these numbers below, it would reach here and
     //  lastDigitSecondNumber=3   and lastDigitFirstNumber=2
     //it has not been able to progress to lastDigitSecondNumber=1 and 
     //lastDigitFirstNumber  since lastDigitFirstNumber has no value.
     //if I try to use these variables again, it would be a loop of errors.
     
     //A solution is to fill a 0 here, but once again I am refraining from this
     //to ensure screen readability mimics calculation in real life.
     
     //number2 = "136";
     //number1 =  "24";
     
     //so now, it can no longer look at lastDigitFirstNumber or lastDigitSecondNumber
     //focus will be on number1 and number2 (string itself.)
      
      try
      {
          System.out.println("FAIL AGAIN");
          System.out.println("number1: " + number1);
          System.out.println("number2: " + number2);
          
          //it shows number1 as  blank
          //it shows number2 as  1
          //NOTE BOTH CAN NOT BE BLANK!
          //String number2 = "136";
          //String number1 =  "24";
          
          System.out.println("This is lastDigitSecondNumber: " + lastDigitSecondNumber);
          System.out.println("This is lastDigitFirstNumber:  " + lastDigitFirstNumber);
          
          //it shows as    3   and    2
          //String number2 = "136";   lastDigitSecondNumber=3
          //String number1 =  "24";   lastDigitFirstNumber=2
          
          
      if (number2!=null)
      {
          //if this is the case, it has to get the last digit
          //and perform addition on this alone as per usual....
          if(number2.length()>1)
          {
              
              
              lastDigitSecondNumberRemainingPortion = (Integer.valueOf(number2)%10);
              total = Integer.valueOf(lastDigitSecondNumberRemainingPortion) + firstDigitTotalDigits;
              
              
               remainingPortionInteger = (int) (Integer.valueOf(number2) / 10);
     remainingPortionString  = Integer.toString(remainingPortionInteger);
     System.out.println("1This is remaining portion after conversion: " + remainingPortionString);
      
      
      System.out.println("length of first number: " + lengthFirstNumber);
      remainingPortion = remainingPortionString.substring(0, (lengthSecondNumber-1));
      System.out.println("This is remaining part: " + remainingPortion);
              
          }
          
          //this would be the condition number2==null)
          //so it would need to undertake manipulation on number1
          
          else
          {
              remainingPortion="";
              total = Integer.valueOf(number2) + firstDigitTotalDigits;
              
          }
          
         
      //at this point we have dealt with scenarios such as these since it has logic
      //to carry forward 1
      //    4636
      //  +  324
         
      //     4636
      //  +   524
         
      //but if the numbers were:    84636
      //                          +   524
      //it would fail since it has not had opportunity to process the 8 in number1
      //so simply, once it has performed total with 4 and no value in number2
      //it needs to take all the remaining numbers in the substring
      //and append it to the grand total
      //in effect it will shorten length number again by 1..
      
      //note this time the shortening is normal.
      
      }
      
      }  //end try 
      
      //exactly same as above, but flipped to number1
      //repeat code but it is required rather than passing value into a 
      //method and creating even more variables!
      catch (NumberFormatException n)
      {
              
      if (number1!=null)
      {
          //if this is the case, it has to get the last digit
          //and perform addition on this alone as per usual....
          if(number1.length()>1)
          {
              
              
              lastDigitFirstNumberRemainingPortion = (Integer.valueOf(number1)%10);
              total = Integer.valueOf(lastDigitFirstNumberRemainingPortion) + firstDigitTotalDigits;
              
              
               remainingPortionInteger = (int) (Integer.valueOf(number1) / 10);
     remainingPortionString  = Integer.toString(remainingPortionInteger);
     System.out.println("1This is remaining portion after conversion: " + remainingPortionString);
      
      
      System.out.println("length of first number: " + lengthFirstNumber);
      remainingPortion = remainingPortionString.substring(0, (lengthFirstNumber-1));
      System.out.println("This is remaining part: " + remainingPortion);
              
          }
          
          //this would be the condition number2==null)
          //so it would need to undertake manipulation on number1
          
          else
          {
              remainingPortion="";
              total = Integer.valueOf(number1) + firstDigitTotalDigits;
          }
          
         
      //at this point we have dealt with scenarios such as these since it has logic
      //to carry forward 1
      //    4636
      //  +  324
         
      //     4636
      //  +   524
         
      //but if the numbers were:    84636
      //                          +   524
      //it would fail since it has not had opportunity to process the 8 in number1
      //so simply, once it has performed total with 4 and no value in number2
      //it needs to take all the remaining numbers in the substring
      //and append it to the grand total
      //in effect it will shorten length number again by 1..
      
      //note this time the shortening is normal.
      
    
      
      }
          
          
          
      }
      
      //this part needs addressing.
      System.out.println("FFFFFtotal: " + grandTotal);
      System.out.println("remaining portion: " + remainingPortion);
      System.out.println("total: " + total);
      
      grandTotal = remainingPortion + Integer.toString(total) + grandTotal;
      
      System.out.println("grand23333: " +grandTotal);
      return grandTotal;
        
    }
        
        return "test";
        
        
    //}  // end of if  number%10!=0
    
    }
}