/*
Online Java - IDE, Code Editor, Compiler

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

//TO PREVENT LOTS REPEAT CODE, ENSURE NUMBER2 IS LONGER THAN NUMBER1
//IT MIGHT GET AWAY IF THERE ARE NO STREAKS OF 9's
//BUT IF NUMBER1 is LONGER AND CONTAINS 9's, I have not coded this.
//Since it will bring lots of duplicated code in a single method.

//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 ensure I can test the execution time, I will add stopwatch..
//I have not attempted this in my code executions so far
//For moment, I am using long variable to ensure I can extend 
//the String to 2,147,483,648 characters (digits only!)

public class Main
{
    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;
    
    static int counter;
    
    //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
    static int firstDigitTotalDigits;
    static int lastDigitTotalDigits;
    static boolean successiveNinesNumber2 =false;
    
    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
    static String remainingPortion;
    
    //if a number is more than 1 digit wider than number 2, 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!
    static int remainingPortionInteger;
    
    //This is conversion of remainingPortionInteger into String 
    static String remainingPortionString;
    
    //exactly this
    static int lastDigitFirstNumberRemainingPortion;
    
    //exactly this
    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
        
        //711 - this is correct
        //String number2 =  "59";
        //String number1 = "652";
        
        //5210 - this is correct
        //String number2 = " 544";
        //String number1 = "4666";
        
        //160 - this is correct
        //String number2 = "136";
        //String number1 =  "24";
        
        //adds extra 2 at front, so adding the total in front was poor logic
        //String number2 = "81636";   
        //String number1 =   "524";
        
        //String number2 = "846369999999999";
        //String number1 =   "32454543";
        
        //all ok
        //String number2 = "9547";   
        //String number1 =   "38";
        
        //all ok, expect no issues...
        String number2 = "9627";   
        String number1 =   "92";
        
        //fail extra 0
        //String number2 = "399947";   
        //String number1 =     "72";
        
        //fail
        //String number2 =   "3970";   
        //String number1 =    "992";
        
        //will flip, if it works, then we know need more code
        //String number1 =   "370";   
        //String number2 =   "192";
        
       
        //String number1 =   "999";   
        //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
        //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.
        
        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 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 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
            
            //REFERS TO NO CARRY FORWARD, THIS SEEMS CORRECT
            //SINCE IT WOULD HAVE NOT BRANCHED OFF TO ANY CATCH STATEMENTS
            if (firstDigitTotalDigits==0)
            {
                System.out.println("GRANDTOTAL: " + grandTotal);
                System.out.println("FINAL TESTING 1");
                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
            else
            {
                System.out.println("FINAL TESTING 2");
                //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("**THIS IS ADDITION********");
      
      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);
           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 test3");
               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
            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=total%10;
        
            //it has to be stored on the most left hand side (MSD) like real life
            grandTotal = Integer.toString(lastDigitTotalDigits) + grandTotal;
          
            System.out.println("running grand total: " + grandTotal);
           
            //recursive call. It truncates numbers by one
            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 MSD
            grandTotal = Integer.toString(total) + grandTotal;
            System.out.println("final test4");
            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);
            
           //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
    //lastDigitFirstNumber=number1.substring(lengthFirstNumber-1);
    //lastDigitSecondNumber=number2.substring(lengthSecondNumber-1);
    //Described above
    
    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****************");
      
      //for an unknown reason, I think it is related to do while loop prior to this
      //an extra zero is being inputted into grandTotal when dealing with streak of
      //9.   Until I find a solution, I will just remove one zero
      
      if (successiveNinesNumber2)
      {
          System.out.println("final test 5");
          System.out.println(lengthSecondNumber);
          
          remainingPortionString = number2.substring(0,lengthSecondNumber-counter+1);
          remainingPortionInteger=Integer.valueOf(remainingPortionString);
          
          System.out.println("REMAINING PORTION: " + remainingPortionString);
          System.out.println("counter: " + counter);
          
          //it would just simply drop remaining aspect of number2 downwards.
          //but it would add 1 onto the last digit in remaining portion.
          
          //it would get last digit on remaining portion
          //lastDigitSecondNumber = remainingPortionString.substring(0,1);
          lastDigitSecondNumberRemainingPortion = remainingPortionInteger%10;
          
          System.out.println(lastDigitSecondNumberRemainingPortion);
          System.out.println("******");
          System.out.println(firstDigitTotalDigits);
          
          //more clarity than doing conversion of data type
          total = lastDigitSecondNumberRemainingPortion + firstDigitTotalDigits;
          
          //this is stripping off the 0, hence taking off 0 from front.
          grandTotal = grandTotal.substring(1);
          
          grandTotal = total + grandTotal;
          
          //now it would take off digit from the remainingPortionString
          //and drop rest numbers down.
          
          //remainingPortionInteger = Integer.valueOf(remainingPortionString);
          
          //remainingPortionInteger = remainingPortionInteger%10;
          
          //grandTotal = total + grandTotal;
          
          System.out.println("END FOR NUMBER 2 ARRANGEMENT, NEEED TO DO SAME NUMBER 1");
          System.out.println(grandTotal);
          
          System.exit(0);
          
      }
      
      //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 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);
          
         //String number2 = "136";   lastDigitSecondNumber=3
          //String number1 = "24";   lastDigitFirstNumber=2
          
      //ie number2 is longer digits wide than number1       
      if (number2!=null)
      {
          System.out.println("final test 6");
          
          //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)
          {
              System.out.println("final test 7 ");
              
              lastDigitSecondNumberRemainingPortion = (Integer.valueOf(number2)%10);
              
              System.out.println("lastDigitSecondNumberRemainingPortion: " + lastDigitSecondNumberRemainingPortion);
      
      //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 difficult to send it back to above loop for recursion...
      //so need to hardcode this logic in again
    //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:" + lastDigitSecondNumberRemainingPortion);
    total = Integer.valueOf(lastDigitSecondNumberRemainingPortion) + firstDigitTotalDigits;
    
    //for example    9647
          //       +   72
          
          //6+ 1 (carried forward) is less than 10.
    
    if (total<10)
    {
        System.out.println("WHAT IS TOTAL: " + total);
        System.out.println("final test 8");
        //drops off last digit remaining portion    
        remainingPortionInteger = (int) (Integer.valueOf(number2) / 10);
        remainingPortionString  = Integer.toString(remainingPortionInteger);
        System.out.println("1This is remaining portion after conversion: " + remainingPortionString);
        
        remainingPortion = remainingPortionString.substring(0, (lengthSecondNumber-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 9 (or infact the entire remainingPortion),  (6+1),  existing grandTotal
        grandTotal = remainingPortion + Integer.toString(total) + grandTotal;
        
    }
    
   
    
    
          
   //total is greater than or equal to 10   (9+1)
   //NOTE, the total can not exceed 10 anymore.
    
    //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!
    
    //for example    9947
          //       +   72
    
    //
    else
    {
        System.out.println("final test 9");
        System.out.println("1GREATER!!!!!!!!!");
        firstDigitTotalDigits=(int) total/10;  //expecting to get a 1
        
        //it could equally be a 0.
        //it still has visibility from the addition on uniform length...
        System.out.println("This is the carry forward: " +  firstDigitTotalDigits);
        
        lastDigitTotalDigits=total%10;  //number between 0 and 8
        //expecting a 1 here
        
        remainingPortionInteger = (int) (Integer.valueOf(number2) / 10);
        System.out.println("should be:   " + remainingPortionInteger);
        
        lastDigitSecondNumberRemainingPortion =Integer.valueOf(Integer.valueOf(number2)%10);
        System.out.println("WHATSSSSS: " + lastDigitSecondNumberRemainingPortion);
        
        
        
        
        
        //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 remainder firstDigitTotalDigits was 0  (6+2=8)
    //The 9 in number.substring(lengthSecondNumber-1) would still be valid
    //since it would not be affected.
    //for instance   9927 
    //              +  62
        
        //one inwards
        if (((Integer.valueOf(number2.substring((lengthSecondNumber-1)))+firstDigitTotalDigits)<10))   
        {
            System.out.println(grandTotal + " so far");
            System.out.println("final test 10");
            System.out.println("£££££££££££££££££");
            //System.out.println(lastDigitSecondNumber);
            
            //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)
             
             //remainingPortionString=number2.substring((lengthSecondNumber-1));
             //remainingPortionInteger=String.valueOf(remainingPortionString);
            
            //total = remainingPortionInteger + firstDigitTotalDigits;
            //grandTotal = Integer.toString(total) + grandTotal;
            //System.out.println("G: " + grandTotal);
            
            //expecting this digit to be subject to remainder if applicable
            //NEED to use remaingPortion here...
            //***HERE***********
            total = lastDigitSecondNumberRemainingPortion + 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 NUMBER2 RIGHT NOW:" + number2);
            
        }
        
        //there will be minimum 1 cascading effect
        //for instance   19947 
        //              +   62
        
        
        else
        {
            //at this point it has to examine all the remainingPortion (199)
            //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 11");
            System.out.println("123****** REMAINING ****:   "  + number2);
            System.out.println(number2.substring((lengthSecondNumber-1)));
            System.out.println(firstDigitTotalDigits);
            counter=0;

            //this can only be a single digit of 9 remaining
            //it would simply perform 9 + 1 = 10.
            //for example 
            //947 
          //+  62
            
            
            if(lengthSecondNumber==1)
            {
                System.out.println("final test 12");
                //total HAS to be 10
                //we expect a record of a carry forward since it would be
                //9 appearing straight away after the lengths processed and differences at uniform location 
                //number1 and number2
                
                remainingPortionInteger=String.valueOf(number2);
                
                //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;
            }
            
            //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 be undertaken by setting up a flag perhaps consecutiveNines
            //and completing this code below in that part..
            //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
            else   
            {
                System.out.println("final test 13");
                
                //in practice it should still have the record of firstDigitTotalDigits
                //and we know it is not 0 otherwise
                //it would have executed this loop:
                //if (((Integer.valueOf(number2.substring((lengthSecondNumber-1)))+firstDigitTotalDigits)<10))
                
                //CAREFUL, but ok to set it again.
                firstDigitTotalDigits=1;
                
                //also need to remember that 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
                remainingPortionInteger = (int) (Integer.valueOf(number2));
                
                System.out.println(number2);
                System.out.println(remainingPortionInteger);
                
                //it takes a digit off the end. It is just to double verify it s a 9s
                //it should be the case
                lastDigitSecondNumberRemainingPortion = (int) (Integer.valueOf(number2) % 10);
                System.out.println("WHAT is: " + lastDigitSecondNumberRemainingPortion);
                
                successiveNinesNumber2=true;
                
                do
                {
                    System.out.println("reminder of : " + lastDigitSecondNumberRemainingPortion);
                    System.out.println("final test 14");
                    
            if (lastDigitSecondNumberRemainingPortion==9)
            {
            
            System.out.println("final 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;
            
            //at same time expecting a 9 to be filled on left hand side and carry over
            //CAREFUL WHAT IS THE PURPOSE OF THIS?
            //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 lastDigitSecondNumberRemainingPortion
            
            //CAREFUL
            //it might be ok infact
            
            total = lastDigitSecondNumberRemainingPortion + 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
            //CAREFUL, does this need to be   %10 ?
            lastDigitSecondNumberRemainingPortion = remainingPortionInteger%10;
            
            counter++;
            System.out.println("\nnew grand total: " + grandTotal);
            System.out.println("ddd: " + number2.charAt(lengthSecondNumber-counter-1));
            System.out.println("COUNTER: " + counter);
            System.out.println("bf: " + lastDigitSecondNumberRemainingPortion);
            
                    
                }while(lastDigitSecondNumberRemainingPortion==9);
            
            System.out.println("final test 16 - !!!!!!OUT OF WHILE LOOP: " + number2);    
            //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
            
            }
            
            
            
        }
        
    }    //end of the big else 
    
    
      
          }
          
          //this would be the condition number2==null)
          //so it would need to undertake manipulation on number1
          
          else
          {
              //the value has to be made blank since it has no content and default it is null
              //this will impact presentation of grandTotal
              remainingPortion="";
              
              //this will just simply take the number and any carry forward from previous MSD
              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;
              
              if (total<10)
              {
              //System.out.println("This is lastDigitFirstNumberRemainingPortion : " + lastDigitFirstNumberRemainingPortion);
              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 is not applicable to both scenarios....
      //this is applicable to if single carry forward does not cause MSD to exceed 
      //9
      grandTotal = remainingPortion + Integer.toString(total) + grandTotal;
      
              
              }
              
    //total is greater than 10
    else
    {
        System.out.println("2GREATER!!!!!!!!!");
        
        firstDigitTotalDigits=(int) total/10;  //expecting to get a 1
        //it could equally be a 0
        
        System.out.println(firstDigitTotalDigits);
        
        lastDigitTotalDigits=total%10;  //number between 0 and 8
        //expecting a 1 here
        
        remainingPortionInteger = (int) (Integer.valueOf(number1) / 10);
        System.out.println("should be 96:   " + 96);
        
        lastDigitFirstNumberRemainingPortion =Integer.valueOf(Integer.valueOf(number1)%10);
        System.out.println("WHATSSSSS: " + lastDigitFirstNumberRemainingPortion);
        
        //lastDigitSecondNumber=String.valueOf(remainingPortionInteger%10;  //should be last digit
        //such as 6
        
        //it would know that even if carry 1 forward was applicable, it would stay single digit
        
        
        System.out.println("CONFIRM RIGHT TRACK: " + number1.substring((lengthFirstNumber-1)));
        
            if ((Integer.valueOf(number1.substring((lengthFirstNumber-1)))<9))   //for example if number1 was 9647 instead of 9947
        {
            System.out.println("$$$$$$$");
            //we know there wont be cascading effect...
            //this would do 6 + carry forward   if (9647 + 72)
            total = Integer.valueOf(number1.substring((lengthFirstNumber-1))) + firstDigitTotalDigits;
            grandTotal = Integer.toString(total) + grandTotal;
            System.out.println("G: " + grandTotal);
            
            //expecting this digit to be subject to remainder if applicable
            
            total = lastDigitFirstNumberRemainingPortion + firstDigitTotalDigits;
            grandTotal = Integer.toString(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 NUMBER2 RIGHT NOW:" + number1);
            
        }
        
        else
        {
            
        }
        
        
        
    }
              
          }
          
          //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);
      
      //EXTRA LINE ADDED - CAREFUL
      //grandTotal = Integer.toString(total) + grandTotal;
      
     
      
      System.out.println("grand23333: " +grandTotal);
      return grandTotal;
        
    }
        
        return "test";
        
        
    //}  // end of if  number%10!=0
    
    }
    }