/*
Online Java - IDE, Code Editor, Compiler

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

//VERSION 2
//I have declared all variables class level and static to ensure
//that the recursive method is kept tidy
//this challenge also raises performance question to capture timings
//to ensure I can test the execution time, I will add stopwatch..

//I used AI solely for this part and realised it was part of Java.lang
//So added start() and stop() around the addition method call in main method 
//this will be my complete last objective.

//For moment, I am using long variable to ensure I can extend 
//the String to 2,147,483,648 characters (digits only!)
//need be careful that MSD can become 10 as oppose to 9
//so allow a single digit buffer for this.
//Also  MSD = most significant digit.   In  25,  2 is MSD.

//NOTE, there will be lots conversions between Integer to String
//(examples remainingPortionInteger => remainingPortionString)
//this is to facilitate appending outcome into grandTotal
//which unfortunately can not be an int or Integer since it does not facilitate
//appending digit at the front.

//NOTE, there will be lots conversions between Integer to String:
//EXAMPLES will be usage of  Integer.toString(integer variable)

//NOTE, there will be lots conversions between String to Integer:
//EXAMPLES will be usage of  String.valueOf(integer variable)

//ALSO the test cases are very aggressive.

public class Main
{
    //used for stop clock
    static long startTime;
    static long endTime;
    static boolean running;
    static long elapsed;
    
    static int lengthFirstNumber; //number digits wide. This will get shorter during recursion.
    static int lengthSecondNumber; //number digits wide. This will get shorter during recursion.
    
    //these will work in conjunction with substring of the number to obtain last digit 
    static String lastDigitFirstNumber;  
    static String lastDigitSecondNumber;
    
    //this is used in area of code that deals with reducing substring
    //of number until last digit of remaining portion is not a nine.
    //it deals with situations where there are two or more consecutive 9s
    static int counter;
    
     //reason for new code v2, dealing with consecutiveNines
    static boolean consecutiveNines;
    
    //I have now removed this, main reason for creation of v2 
    static int temp;
    
    //extremely difficult to provide meaningful name
    //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
    //crucial part of adding two digits together
    //In practice this is also the carry forward
    static int firstDigitTotalDigits;
    
    //9 + 3 = 12     firstDigitTotalDigits=1   lastDigitTotalDigits = 2
    static int lastDigitTotalDigits;
    
    static int total; //stores total of two digits
    
    //used to output the running grandtotal
    //similar to expectation of manually completing addition
    static String grandTotal=""; 
    
    //all below variables are closely linked
    //in addition, if a number is wider with extra digits, those additional
    //MSD need to drop straight down into grandtotal under right conditions
    static String remainingPortion;
    
    //if a number is more than 1 digit wider than other number, 
    //the LSD of this portion has to be excluded. (since in real life addition, this is subject to 
    //change if a number is carried forward).
    //if for instance, this is 9, then 9+1 =0, and carry 1.
    //in practice this could go on if all the digits in remainingPortionInteger is 9!
    //so would need to catch this ArrayIndexOutOfBoundsException should
    //String exceed limit of the long
    static int remainingPortionInteger;
    
    //This is conversion of remainingPortionInteger into String 
    static String remainingPortionString;
    
    //exactly this, least MSD of remaining portion of String number1
    static int lastDigitNumberOneRemainingPortion;
    
    //exactly this, least MSD of remaining portion of String number2
    static int lastDigitNumberTwoRemainingPortion;

     //these come into play in the first catch statement...
     //it does not know which caused the exception.
     //so the variable names need to be generalised to ensure user friendly name
     //to support calculations...
     static String number;
     static int lastDigitRemainingPortion;
     static int lengthNumber;
     static String lastDigitNumber;
    
    
    public static void start() 
    {
        Main.startTime = System.nanoTime();
        Main.running = true;
    }

    public static void stop() 
    {
        Main.endTime = System.nanoTime();
        Main.running = false;
    }

    public static long getElapsedTime() 
    {
        if (running) {
            elapsed = System.nanoTime() - startTime;
        } 
        else 
        {
            elapsed = endTime - startTime;
        }
        return elapsed;
    }

    public static double getElapsedTimeInSeconds() 
    {
        return getElapsedTime() / 1_000_000_000.0;
    }
    
    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");
        
        //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.
        //EXPECT ERRORS IF ATTEMPTED
        //THERE ARE LOTS TEST CASES, SINCE IT WAS REALISED THAT WHEN FIXING CODE,
        //CASES FAILED AND THEN PASSED, AND VICE VERSA
        //ALSO CAN CHANGE VARIABLE number2 to number1 and vice versa.
        
         //***PASS****  - 100*** via final test
        //String number2="99";
        //String number1= "1";
        
        //***PASS**1764*** via final test
        //String number2="993";
        //String number1="771";
        
        //PASS*****160****** - via final test 
        //String number2 = "136";
        //String number1 =  "24";
        
        //PASS******846370032454542****** via final test 
        //String number2 = "846369999999999";
        //String number1 =        "32454543";
        
        //PASS****82160**** via final test 
        //String number2 = "81636";   
        //String number1 =   "524";
        
        //PASS******* - 400219  via final test 
        //String number2 = "399627";   
        //String number1 =    "592";
        
        //pass********* - 400019 via final test 
        //String number2 = "399947";   
        //String number1 =     "72";
        
        //PASS***4962****** getting via final test 
        //String number2 =   "3970";   
        //String number1 =    "992";
        
        //PASS******  562  via final test 
        //String number1 =   "370";   
        //String number2 =   "192";
        
        //PASS**** - 1000  via final test 
        //String number2 =   "999";   
        //String number1 =     "1";
        
        //PASS*****  1998 via final test 
        //String number1 =   "999";   
        //String number2 =   "999";
        
        //PASS ******* 1098 via final test 
        //String number1 =   "999";   
        //String number2 =    "99";
        
        //***PASS***108 via final test 
        //String number1 =   "99";   
        //String number2 =   " 9";
    
         //***PASS**** 1400019  via final test
         //String number2 = "1399947";   
         //String number1 =      "72";
         
        //***PASS****5210*** via final test 
        //String number2 = "4666";
        //String number1 =  "544";
        
        //***PASS***711**** via final test 
        //String number2 =  "652";
        //String number1 =   "59";
        
        //PASS****1390219 ***** via final test 
        //String number2 = "1389627";   
        //String number1 =     "592";        

        //PASS***********22023**** via final test 
        //String number1 = "21999";   
        //String number2 =     "24";
        
        //PASS****2*******  via final test 
        //String number1 = "1";   
        //String number2 = "1";
        
        //PASS***10********  via final test 
        //String number1 = "1";   
        //String number2 = "9";
        
        //PASS***95759******** via final test 
        //String number1 = "95427";   
        //String number2 =   "332";
        
        //PASS*****9719******   via final test
        //String number1 = "9647";   
        //String number2 =   "72";
        
        //PASS****100119*******   via  final test 
        //String number1 = "99647";   
        //String number2 =   "472";
        
        //   **PASS** 10009 via  final test 
        //String number1 ="9947"; 
        //String number2 = " 62";
        
        //PASS via  final test 
        //***PASS  22023
        //String number1 = "21999";   
        //String number2 =     "24";
        
        //PASS via  final test 
        //String number1 = "99";   
        //String number2 =  "9";
        
        //PASS getting 1000
        //String number1 = "999";
        //String number2 = "1";
        
        //PASS via final test 3,5,6,7,8,10,11,12,27,
        //String number1 = "888";
        //String number2 = "1";
        
        //PASS  via final test 0,2,3,4,27
        //String number1 = "99999999999999999999999999999999999999999999999999999999";
        //String number2 = "32423543534543532423432423432494355443534532235324324333";
        
        
        //**************INTRODUCING FEW EXTRA TEST CASES
        
        //***PASS   1000000  via final test  3,4,6,7,8,10,11,13,15,17,18,19,22,23,26,27
        //String number1 = "999999";
        //String number2 = "1";
        
        //****PASS  10001424201998***  via final test 3,4,5,6,7,8,10,11,13,15,17,18,19,23,26,27
        ///String number1 = "9999990767676";
        //String number2 = "1433434322";
        
        //****FAIL  giving 97***  NOW FIXED
        //String number1 = "9999990767676";
        //String number2 = "1";
        
        //****FAIL  giving 97***  NOW FIXED
        //String number1 = "7899990767676";
        //String number2 = "1";
        //hence issue with transition from non-9s to 9s
        //so I am getting the code to print GrandTotal
        //at all the final test locations in which
        //it modifies the grandTotal
        //it is bizarre that it has not shown system.out.println() other
        //than grandTotal=7
        
        
        //FAIL  ***97****
        //if I decrease the number of 9s to 2, 
        //execution is with issue
        //it currently passes via  3,5,7,8,10,11,21,22,24,26,27
        //it can be seen via system.out.println() that it enters final test 11
        //but for some reason, there is code in there including further outputs to screen.
        //it does not even finish the code segment.
        //I removed each line at a time and figured out the following line:
        //lastDigitRemainingPortion = (Integer.valueOf(number)%10)  executed
        //and then caused the whole block to skip...
        //it also acts similarly with
        //remainingPortionInteger =  (Integer.valueOf(number));
        //but it has a successful value for number when printed to the screen
        //(78990767676).
        //There were several mistakes and I was still using lastDigitFirstNumber
        //when this was phased out...
        //PASS
        //String number1 = "78990767676";
        //String number2 = "1";
        
        
        //****PASS  giving 97653***  via final test 3,5,6,7,8,10,11,27
        //String number1 = "97652";
        //String number2 = "1";
        
        
        //For an unknown reason, the addition method is unable to return the String, it just shows blank value
        //or just skips the return statement (proven by recursive method terminating) and returning test
        //to method invocation in main.
        //Good news is that it does not branch off elsewhere in the code.
        //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) in recursive method.
        //it will be proven since the code will officially return last return statement.
        
        start();
        System.out.println(number1 + "+" + number2 + "= " + addition(number1, number2));
        System.out.println(number1 + "+" + number2 + "= " + grandTotal);
        stop();
        System.out.println("Elapsed time in seconds: " + getElapsedTimeInSeconds());
    }
    
    public static String addition(String number1, String number2)
    {
        //it is still working constraints of the code
        //the Strings are not being converted to simplify addition, 
        //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 carry forward 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)
      {
          System.out.println("final test 0");
          System.out.println("GRANDTOTAL*****: " + grandTotal);
           //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
            
            //REFERS TO NO CARRY FORWARD, THIS SEEMS CORRECT
            //SINCE IT WOULD HAVE NOT BRANCHED OFF TO ANY TRY/CATCH STATEMENTS
            if (firstDigitTotalDigits==0)
            {
                
                System.out.println("final test 1");
                System.out.println("GRANDTOTAL*****: " + grandTotal);
                return grandTotal;
            }
            
            // THIS APPEARS CORRECT SINCE IT WOULD HAVE NOT HIT ANY CATCH STATEMENT
            //SO IT IS IN MAIN CODE FLOW
            //IT WOULD HAVE ADDED lastDigitTotalDigits into grandTotal
            //just a case of adding extra 1 since no more digits left in number1 or number2
            //AND if the firstDigitTotalDigits is not 0, it has to be 1
            else
            {
                System.out.println("FINAL TESTING 2");
                //it would append a 1 to the left of existing MSD
                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
      
      System.out.println("**THIS IS ADDITION**********");
      System.out.println(number1);
      System.out.println(number2);
        
      //need try here since it was giving NumberFormatException and
      //StringIndexOutOfBoundsException (explained in catch statement)
      //why these could occur
      
      //note, it would have already dealt with circumstance of processing all
      //digits in both uniform numbers above in first loop in addition method.
      //So would not be here if this was not case
      
       try
       {
           System.out.println("final test 3");
           System.out.println("GRANDTOTAL*****: " + grandTotal);
           //if number1 was 1234 lengthFirstNumber=4
           //performing number1.substring(3) would get single digit from index position 3
           //note that number1 and number2 are shrinking as part of the process
           //so it always relative to same last index
           //it replicates simulation of moving across right to left in real life
           lastDigitFirstNumber=number1.substring(lengthFirstNumber-1);
           lastDigitSecondNumber=number2.substring(lengthSecondNumber-1);
           
           //as you would expect to add those digits above
           total = Integer.valueOf(lastDigitFirstNumber) + Integer.valueOf(lastDigitSecondNumber) + firstDigitTotalDigits;
           
           //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)
           {
               System.out.println("\nOVER 10");
               System.out.println("final test 4");
               System.out.println("addition of: " + Integer.valueOf(lastDigitFirstNumber) + " + " + Integer.valueOf(lastDigitSecondNumber));
               System.out.println("this is current total: " + total);
            
               //first digit carried forward, only feasible value is 1 since total greater than 10
               firstDigitTotalDigits = (int)total/10;
               System.out.println("This is first digit, it will be carried forward: " + firstDigitTotalDigits);
            
               //getting last digit in total (10-18),  lastDigitTotalDigits is between 0-8
               lastDigitTotalDigits=total%10;
        
               //it has to be stored in grandTotal on the most left hand side (MSD) like real life
               //note it is not overwriting existing value
               grandTotal = Integer.toString(lastDigitTotalDigits) + grandTotal;
          
               System.out.println("GRANDTOTAL*****: " + grandTotal);
           
               //recursive call. It truncates numbers by one digit as described above
               addition(number1.substring(0, (lengthFirstNumber-1)), number2.substring(0, (lengthSecondNumber-1)));
           }
           else
           {
               //we expect total to be a single digit
               ///same principle above, added to left of existing MSD
               grandTotal = Integer.toString(total) + grandTotal;
               System.out.println("final test 5");
               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("GRANDTOTAL*****: " + grandTotal);
               
               //it needs to set the carry forward value to 0
               firstDigitTotalDigits=0;
               
               //same style recursive call
               addition(number1.substring(0, (lengthFirstNumber-1)), number2.substring(0, (lengthSecondNumber-1)));
            }
       }
       
       //it would enter here if issue processing:
       //REASON for StringIndexOutOfBoundsException if either
       //lengthFirstNumber or lengthSecondNumber were 0
       //lastDigitFirstNumber=number1.substring(lengthFirstNumber-1);
       //lastDigitSecondNumber=number2.substring(lengthSecondNumber-1);
       
       //REASON for NumberFormatException
       //Since if no value in either (NOT BOTH) Strings (lastDigitFirstNumber
       //or lastDigitSecondNumber) 
       // it can not get Integer value henceforth.
       //Integer.valueOf(lastDigitFirstNumber)
       //Integer.valueOf(lastDigitFirstNumber)
       
       catch (NumberFormatException | StringIndexOutOfBoundsException s)
       {
            System.out.println("123This is last digit first number:" + lastDigitFirstNumber);
            System.out.println("123This is last digit second number:" + lastDigitSecondNumber);
            System.out.println("final test6");
        
            //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()
            //it is imaginary thought process and perhaps would have solved lots of these
            //exceptions. But I feel other logic going forward might have been disrupted also.
      
            //still in catch, processing non uniform numbers..
      
            System.out.println("number 1 at this point: " + number1);
            System.out.println("number 2 at this point: " + number2);
   
            //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 examine lastDigitFirstNumber or lastDigitSecondNumber
            //focus will be on number1 and number2 (string itself.)
            System.out.println("GRANDTOTAL*****: " + grandTotal);
      
            try
            {
                System.out.println("final test 7");
                System.out.println("number1: " + number1);
                System.out.println("number2: " + number2);
          
                //NOTE BOTH CAN NOT BE BLANK!
                System.out.println("This is lastDigitSecondNumber: " + lastDigitSecondNumber);
                System.out.println("This is lastDigitFirstNumber:  " + lastDigitFirstNumber);
          
                System.out.println("CARRY FORWARD RIGHT NOW: " + firstDigitTotalDigits);
      
                //it is not sure which number is null, so rather than copying the 
                //code in if number2!=null   into similar if statement (number1!=null), 
                //variables created to be generic to both scenarios:
                //this will also allow me to flip the variable names in test case variable declarations
                //without worrying if number1 is longer than number2 or which has streaks of 9
      
                System.out.println("num1: "+ number1);
                System.out.println("num2: " + number2);
                
                //length of one number will be greater than the other
                //number variable is just the remaining portion of number.
                //lastDigitRemainingPortion variable takes % of number
                //lengthNumber is length of the number
                System.out.println("GRANDTOTAL*****: " + grandTotal);
                
                if (number1.length()>number2.length())
                {
                    System.out.println("final test 8");
                    System.out.println("GONE INTO NUMBER1 NOT NULL");
                    number=number1;
                    lastDigitRemainingPortion = lastDigitNumberOneRemainingPortion;
                    lengthNumber=lengthFirstNumber;
          
                    //note this variable is opposite, will be evident later
                    lastDigitNumber=lastDigitSecondNumber;
                    System.out.println("GRANDTOTAL*****: " + grandTotal);
                    
                }
                
                //same explanation as above for variables
                if (number2.length()>number1.length())
                {
                    System.out.println("final test 9");
                    System.out.println("GONE INTO NUMBER2 NOT NULL");
                    number=number2;
                    lastDigitRemainingPortion = lastDigitNumberTwoRemainingPortion;
                    lengthNumber=lengthSecondNumber;
          
                    //note this variable is opposite, will be evident later
                    lastDigitNumber = lastDigitFirstNumber;
                    System.out.println("GRANDTOTAL*****: " + grandTotal);
                }
                
                //it no longer matters if EITHER number1 or number2, ready to handle
                //both scenarios
                
                if (number!=null)
                {
                    System.out.println("final test 10");
                    System.out.println("WHAT IS NUMBER:" + number);
                    
                    //if this is the case, it has to get the last digit
                    //and perform addition on this alone as per usual....
                    System.out.println("GRANDTOTAL*****: " + grandTotal);
                    
                    if(number.length()>1)
                    {
                        System.out.println("final test 11");
                        System.out.println("final again");
                        
                        
                        
                        System.out.println("final again1 with number: " + number);
                        //taking % gets last digit
                        //**HAVING THIS CODE CAUSED THE WHOLE BLOCK TO JUMP OUT
                        //THE LOOP... no known cause
                        //lastDigitRemainingPortion = (remainingPortionInteger%10);
                        //OR
                        //lastDigitRemainingPortion=String.valueOf(number%10;
                        //remainingPortionInteger =  (Integer.valueOf(number));
                        //remainingPortionString  = Integer.toString(remainingPortionInteger);
                        
                        System.out.println("LENGTH NUM: " + lengthNumber);
                        lastDigitNumber=number.substring(lengthNumber-1);
                        System.out.println("LN: " + lastDigitNumber);
                        
                        
                        System.out.println("RP:" +remainingPortionInteger);
                        
                        System.out.println("lastDigitRemainingPortion: " + lastDigitRemainingPortion);
                        
                        //at this point, we still need to 
                        //have lots consideration. Before above it would carry across the
                        //firstDigitTotalDigits (which would be 1 if total is 10-18)
                        //if no extra logic is applied, it will perform:
                        //String number2 = "9947";   
                        //String number1 =   "72";
      
                        //7+2  = 9   (grandtotal = 9)
                        //4+7=  11   (store 1 and carry 1 across)   (grandtotal = 19)
                        //9+1  = 10  (it will not carry anything)  (grandtotal = 1019)  (INCORRECT)
                        //drop the last digit number1    (grandtotal = 91019)  (INCORRECT)
      
                        //it is NOW difficult to send it back to above loop for recursion...
                        //option is to create a boolean flag and force it back here.
                        //better option for understanding is to hardcode this logic in again
                        //and take a performance hit.
                        //if total is less than 10, it can simply get total and drop numbers like above
    
                        //total as usual, full visibility of firstDigitTotalDigits from legitimate calculation
                        //on last digit at uniform location in number1 and number2
    
                        System.out.println("lastdigit remaining portion:" + lastDigitNumber);
                        total = Integer.valueOf(lastDigitNumber) + firstDigitTotalDigits;
    
                         //for example explaining below:    9647
                        //                                +   72
                        //6+ 1 (carried forward) is less than 10.
                        System.out.println("GRANDTOTAL*****: " + grandTotal);
                        System.out.println("TOTAL*****: " + total);
                        
                        
                        if (total<10)
                        {
                            System.out.println("WHAT IS TOTAL: " + total);
                            System.out.println("final test 12");
                            
                            //drops off last digit remaining portion
                            //same issue as above, it can not handle and jumps loop
                            //remainingPortionInteger = (Integer.valueOf(number) / 10);
                            //remainingPortionString  = Integer.toString(remainingPortionInteger);
                            
                            //since lengthNumber-1 is the position of last digit, it will exclude this
                            //not looking to truncate
                            remainingPortionString=number.substring(0,(lengthNumber));
                            
                            //System.out.println("1This is remaining portion after conversion: " + remainingPortionString);
        
                            remainingPortion = remainingPortionString.substring(0, (lengthNumber-1));
                            System.out.println("This is remaining part: " + remainingPortion);
    
                            //this is not applicable to both scenarios....
                            //this is applicable to if single carry forward does not cause MSD to exceed 
                            //9
      
                            //it can simply drop down 9 (as per example outside this loop) 
                            //(or infact the entire remainingPortion) since total (6+1 is less than 9)
                            grandTotal = remainingPortion + Integer.toString(total) + grandTotal;
                           System.out.println("GRANDTOTAL*****: " + grandTotal);
                            
                            return grandTotal;
                            
                        }
                        
                        //total is greater than or equal to 10   (inner 9+1)
                        //NOTE, the total can not exceed 10 anymore
                        //since 1 is maximum firstDigitTotalDigits
                        //and not uniform number1 and number2
    
                        //this else loop will get very involved later on.
                        //since the carry forward could have a knock on effect
                        //if the upcoming MSD are 9's! 
                        //number1 = "99647";   
                        //number2 =   "472";
                        
                       else
                       {
                           System.out.println("final test 13");
                           System.out.println("1GREATER!!!!!!!!!");
                           
                           firstDigitTotalDigits=(int) total/10;  //expecting to get a 1
                           System.out.println("This is the carry forward: " +  firstDigitTotalDigits);
        
                           lastDigitTotalDigits=total%10;  //number between 0 and 8
                           
                           //expecting a 1 here since it has removed lastDigitTotalDigits
                           remainingPortionInteger = (int) (Integer.valueOf(number) / 10);
                           System.out.println("should be:   " + remainingPortionInteger);
                           
                           //we need this number to ascertain what actions required with a carry forward
                           //if this is a 9, we know that firstDigitTotalDigits is definitely 1, 
                           //it will force 9+1 and store 0 into grandTotal
                           lastDigitRemainingPortion =Integer.valueOf(Integer.valueOf(number)%10);
                           System.out.println("WHATSSSSS: " + lastDigitRemainingPortion);
        
                           //NOT ENTIRELY SURE IF THIS IS CORRECT EXAMPLE//
                           //it would enter in similar situation      but not 
      //                    "9627"                    "927"   
     //                   +   "92"                  +  "92"
    
                        //System.out.println("CONFIRM RIGHT TRACK TOTAL GREATER THAN 10: " + number2.substring((lengthSecondNumber-1)));
    
                        //no cascading effect
                        //it would know that even if carry 1 forward was applicable, 
                        //it would stay single digit
    
                        //NEED to be careful and understand that if carry forward firstDigitTotalDigits was 0  (6+2=8)
                        //The 9 in number.substring(lengthSecondNumber-1) would still be valid
                        //since it would not be affected.
                        
                        //IT IS BIT ODD THAT THIS NEXT CONDITION BLOCK WAS A VALID CODE SEGMENT DURING MODIFYING MY CODE TO COMPLETION,
                        //BUT CAN NOT FORCE IT BACK HERE AGAIN
                        //IT HAS BEEN OVERRIDEN SOMEWHERE, AT MOMENT JUST NOT SURE
                        //IT FAILS TO ENTER HERE IN ALL MY TEST CONDITIONS
                        //FOR MOMENT I AM MANTAINING THE CODE
                        
                        //Tried to investigate exactly where it was at
                        //System.out.println("still ft9: " + number);
                        //System.out.println("still ft9 carry forward: " + firstDigitTotalDigits);
                        //System.out.println("still ft9: " + (lengthNumber-1));
                        //System.out.println("still ft9: " + Integer.valueOf(number.substring((lengthNumber-1))));
                        //System.out.println(remainingPortionInteger);
                        System.out.println("GRANDTOTAL*****: " + grandTotal);
                        
                        if (((Integer.valueOf(number.substring((lengthNumber-1)))+firstDigitTotalDigits)<10))   
                        {
                            System.out.println(grandTotal + " so far");
                            System.out.println("final test 14");
                            System.out.println("£££££££££££££££££");
                            //System.exit(0);
                        
                            //we know there wont be cascading effect...
                            //this would do 6 + carry forward), 
                            //remainingPortion(MSD) into grandtotal. On this instance it would be 9
                            //(9647
                            // + 72)
             
                            total = lastDigitRemainingPortion + firstDigitTotalDigits;
            
                            //this will put the remainingpart(9) + total (9 + 0 carried forward) + existing grandTotal
                            grandTotal = remainingPortionInteger + total + grandTotal;
                            System.out.println("G: " + grandTotal);
            
                            //it would now truncate single digit of number 2
                            //and place this in the grandtotal
            
                            System.out.println("WHAT IS NUMBER RIGHT NOW:" + number);
                            System.out.println("GRANDTOTAL*****: " + grandTotal);
                        }
                        
                        //there will be minimum 1 cascading effect
                        //for instance   19947 
                        //              +   62
                        
                        else
                        {
                            //at this point it has to examine all the remainingPortion (19)
                            //backwards since if there are multiple 9 this will keep cascading...
                            //a good starting point is being here and acknowleding there is a single cascading effect
                            //at minimum
            
                            System.out.println("final test 15");
                            //System.out.println(remainingPortionInteger);
                            System.out.println("123****** REMAINING ****:   "  + number);
                            System.out.println(number.substring((lengthNumber-1)));
                            System.out.println(firstDigitTotalDigits);
                            
                           //this can only be a single digit of 9 remaining
                           //it would simply perform 9 + 1 = 10.
                           //for example
                             //947
                           //+  62
                           
                           System.out.println("GRANDTOTAL*****: " + grandTotal);
                           
                           if(lengthNumber==1)
                           {
                               System.out.println("final test 16");
    //total HAS to be 10
    //we expect a value in carry forward firstDigitTotalDigits since it would be
    //9 appearing straight away after the lengths processed (at uniform MSD location) in 
    //number1 and number2
                
                            remainingPortionInteger=Integer.valueOf(number);
                
            //there is a single digit hence number2 can be taken as a whole.
                            total = remainingPortionInteger + firstDigitTotalDigits;
                
            //it can now be placed into grandtotal
                            grandTotal = Integer.toString(total) + grandTotal;
                            System.out.println("GRANDTOTAL*****: " + grandTotal);
                            }
            
            //this is almost a recursive do while loop without calling
            //recursive method.
            //I am not sure if this is within restrictions of the challenge
            //but its not practical to call the addition recursive method again..
            //it can perhaps be undertaken by setting up a flag perhaps consecutiveNines
            //but I think its best to keep flow going downwards than to move upwards..
            
            //more than 1 digit remaining
            //and whilst they are all 9s
            //same scenario as above but extra 9
             //9947
            //+  62
            //This was the most challenging part of the code.
            //It has created some disagreement in my thought process
            //but it was expected when branching this deep.
            
                            else
                            {
                                System.out.println("final test 17");
                
        //in practice it should still have the record of firstDigitTotalDigits
        //and we know it is not 0 otherwise
                
        //it would have executed this loop (since 9 + 0 <10)
        //if (((Integer.valueOf(number2.substring((lengthSecondNumber-1)))+firstDigitTotalDigits)<10))
                
        //ok to set it again, but this only adds to confusion. Not required
                                firstDigitTotalDigits=1;
                
        //also need to remember that currently still within
        //massive else statement of the extra digit in number2 equalling
        //10 (with 1 carry forward).
                
        //for instance  the 39 part left of the number
        //    3974 
        //   +  45
        
        //This now includes entire block
        //note number2 has not got shorter at all yet
                
        //It might be worth looking at addition such as, which would bring us here
        //number1 = "21999";   
        //number2 =     "24";
        //these are key variables which dictate calculations...
        //Grand total is   23
        //Number so far is 219
        //remainingPortionInteger = 21
        //this tells us no processing has occured for the 9s
                                System.out.println("GRAND TOTAL SO FAR: " + grandTotal);
                                System.out.println("NUMBER SO FAR: " + number);
                                System.out.println("REMAINING PORTION INTEGER SO FAR: " + remainingPortionInteger);
                                System.out.println("last digit remaining portion: " + lastDigitRemainingPortion);
                
                                //this now removes the last digit.
                                remainingPortionInteger = (int) (Integer.valueOf(number))/10;
                
                                //since it has not processed the digit removed above from number,
                                //it needs to store it first. It would be storing 9 for instance.
                                //temp=Integer.valueOf(number)%10;
                
                                System.out.println("The number:" + number);
                                System.out.println("remaining portion integer:" + remainingPortionInteger);
                
                                //it takes a digit off the end off 21.
                                //lastDigitRemainingPortion = remainingPortionInteger % 10;
                                System.out.println("WHAT is lastDigitRemainingPortion now: " + lastDigitRemainingPortion);
                
                                //this now processes the number of consecutive 9s found
                                do
                                {
            //still at this point, based on:
            //number1 = "21999";   
            //number2 =     "24";
            //remainingPortionInteger = 21
            //grand Total = 23
            //lastDigitRemainingPortion = 1
            //So it will skip this next if loop (final test 15)
            //but it has to be questioned, why was last digit removed
            //if it enters this loop for the first 9 situation with
            //consecutive 9s, it will also shrink the variable
            //remainingPortionString (based on remainingPortionInteger)
            //I have tried to comment out the lines above:
            //temp=Integer.valueOf(number)%10;
            //lastDigitRemainingPortion = remainingPortionInteger % 10;
            //But what happens is that it breaks out of the do while loop
            //it reaches bottom of code and performs:
            //grandTotal = total + grandTotal;  We know it should actually
            //drop the (remainingPortionInteger + 1)  =  22
            //But instead it drops 10,   so the answer for  21999+24 = 1023
            //I also set up a boolean so that it can apply correct logic
            //if it went into lastDigitTotalDigits=9  (i.e commenting out lastDigitTotalDigits).
            //in which case grandTotal would be  019
            //and if the flag is set it would drop the  22 in front of 019
            //I have created a version 2 here to support this change
                
                                System.out.println("\nfinal test 18");
                                System.out.println("remainportion: " + remainingPortionInteger);
                                System.out.println("GRAND TOTAL---:" + grandTotal);
                                System.out.println("last dig pos: " + lastDigitRemainingPortion);
                                System.out.println("\n\nfinal test 15 - definitely a 9");
                                //we know this can only be as a result of  9+1
                                total = 0;   // (equivalent to  9+1 and obtaining last digit)
            
                                //this begins to write a 0 for every 9 that it finds in remainingPortionInteger
                                grandTotal = Integer.toString(total) + grandTotal;
                                
                                //this will get shorter by a digit each time
                                remainingPortionInteger = remainingPortionInteger/10;
                    
                                //to speed up execution, this is also better option		   
                                //total = 0;   // (equivalent to  9+1 and obtaining last digit)
		                        //prevents calculation below.
           
                                //at same time expecting a 9 to be filled on left hand side and carry over
                                //this is so that once it breaks out of the loop,
                                //it has a total of MSD before the 9.
                                //We know the previous MSD would have been 9+1 = 0
                                //so it includes the lastDigitNumberTwoRemainingPortion
                                total = lastDigitRemainingPortion + firstDigitTotalDigits;
            
                                //it will now get new last digit
                                //the purpose of this is so that once it finishes the do while loop
                                //of storing 9s, it can process anything else left
                                //remainingPortionInteger
                                lastDigitRemainingPortion = remainingPortionInteger%10;
                                System.out.println("bf: " + lastDigitRemainingPortion);
                                
                                //to ensure it can extract right number once consecutiveNines loop
                                //processed below. Unfortunately this still violates recursion.
                                counter++;
                        
                                //this flag will be used later on
                                consecutiveNines=true;
                                System.out.println("GRANDTOTAL*****: " + grandTotal);
                    
                        }while(lastDigitRemainingPortion==9);
                        
                        System.out.println("final test 19");    
            //at this point the remaining number2 could be:
            //8   or  it could be   888888888
            //But we know definitely that there is no longer a cascade for the
            //grandtotal
            //so it would add tota
                        System.out.println("remain:" + remainingPortionInteger);
            
                        remainingPortionString=Integer.toString(remainingPortionInteger);
                        System.out.println("GRANDTOTAL*****: " + grandTotal);
            
                        }//end of else  final test 17
            
                        }//end else final test 15 minimum 1 cascading effect   
        
                    }    //end of else final test 13 (total>10) 
                        
                    }    // this would be if final test 11 if(number.length()>1)
          
                    //this would be else of the above.
                    //so number.length<=1
          
                    else
                    {
                        System.out.println("DOUBLE CHECK CARRY FORWARD: " + firstDigitTotalDigits);
                        System.out.println("final test 20");
              //the value has to be made blank since it has no content and default it is null
              //this will impact presentation of grandTotal
                        System.out.println(lastDigitNumber);
                        total = Integer.valueOf(lastDigitNumber) + firstDigitTotalDigits;
                        System.out.println("uuTOTAL NOW: " + total);
              
                        remainingPortionInteger=  Integer.valueOf(number);
                        System.out.println("REMAINING PORTION: " + remainingPortionInteger);
              //this will just simply take the number and any carry forward from previous MSD
                        total = remainingPortionInteger + firstDigitTotalDigits;
                        System.out.println("TOTALLLL: " + total);
                        grandTotal = total + grandTotal;
                        System.out.println("GRANDTOTAL*****: " + grandTotal);
                        
                        return grandTotal;
                    }
          
          //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 is the end of final test 10   if (number!=null)
      
            }  //end try      final test 7 
      
      //exactly same as above, but flipped to number1
      //catch (NumberFormatException | StringIndexOutOfBoundsException s)
      //I have bypassed this by setting up generic variables as described in declarations
            catch (NumberFormatException n)
            {
                System.out.println("final test 21");
                //this was previously reserved for performing operation when
                //number1!=null
                //but I changed logic as described in the try.
                //so strictly speaking, the whole try structure above can be removed.
                System.out.println("GRANDTOTAL*****: " + grandTotal);
            }
            System.out.println("final test 22");
            //this part needs addressing.
            System.out.println("FFFFFtotal: " + grandTotal);
            System.out.println("remaining portion: " + remainingPortion);
            System.out.println("total: " + total);
            System.out.println("cvalue of consec nines: " + consecutiveNines);
      
            //THIS IS MAIN PART OF NEW CODE IN VERSION 2
      
      //if there are consecutive nines, the total has to be adjusted.
      // it is no longer the total that was taking place at uniform MSD of the numbers
      //it is the total that appears in MSD after all 9s processed
      //the firstDigitTotalDigits (carry forward) will still be present.
      System.out.println("GRANDTOTAL*****: " + grandTotal);
      
            if (consecutiveNines)
            {
                System.out.println("final test 23");
                total = Integer.valueOf(number.substring(0,lengthNumber-counter)) + firstDigitTotalDigits;
                System.out.println("GET HERE:");
                grandTotal = total + grandTotal;
                System.out.println("GRANDTOTAL*****: " + grandTotal);
                
            }
      
          //if it did not enter that section for instance  99 + 9  (since 9+9) was processed as normal part
          //of addition
          //it needs to not process the total since this would have been 9+9
          //the total would 9 (since there is no trace of this number in variables) + 1
      
            else
            {
                 if (remainingPortion==null)
                 {
                    System.out.println("final test 24");
                    System.out.println("GET HERE1:");
                    total = 9 + firstDigitTotalDigits;
                    grandTotal = Integer.toString(total) + grandTotal;
                    System.out.println("GRANDTOTAL*****: " + grandTotal);
                }
          
              //if it was a general number without consecutive nines and also without a single
              //9 in MSD (outside of the uniform number1 and number2)
              //normal procedure
                else
                {
                    System.out.println("final test 25");
                    System.out.println("get here2");
                    grandTotal = total + grandTotal;
                    System.out.println("GRANDTOTAL*****: " + grandTotal);
                }
            }
            
                    //hard to explain reason for this code
                    //would expected everything to be in if-else
                    //would need to verify it enters here part of testing.
                    System.out.println("final test 26");
                    System.out.println("GRANDTOTAL*****: " + grandTotal);
      
                    System.out.println("this is remainig: " + remainingPortionInteger);
      
                    System.out.println("grand23333: " +grandTotal);
      
                    return grandTotal;
    //end of final test 6    
    }   //end of    catch (NumberFormatException | StringIndexOutOfBoundsException s)
        
        System.out.println("final test 27");
        System.out.println("GRANDTOTAL*****: " + grandTotal);
        return "test";
    
    }  //end of main method
    
}//end of class