
/*
Online Java - IDE, Code Editor, Compiler

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

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;
    
    public static void main(String[] args) {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        String number1 = "84636";
        String number2 = " 324";
        
        addition(number1, number2);
    }
    
    public static String addition(String number1, String number2)
    {
        System.out.println("Another chance");
        //if it has not run out of digits in the number
        //for instance here it have to end when either hit 1 digit wide
         //if ((Integer.valueOf(number1)%10!=0) || (Integer.valueOf(number2)%10!=0))
         //{
        
        //try
        //{
        lengthFirstNumber=(number1.length());
        //}
        
        //catch (NumberFormatException e)
        //{
            //System.out.println("No more digits left: " + number1);
        //}
        
        //try
        //{
        lengthSecondNumber=(number2.length());
        //}
        //catch (NumberFormatException e)
        //{
            //System.out.println("No more digits left: " + number2);
            
        //}
        
        /*
        
        //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
                                +     
        
        //this is just a case of dropping the remainder across
        //for instance performing  55
                                 + 55
                                 -------
                                  110
                                 -------
                                 
        if (number2%10==0 && number1%10==0) &&   firstDigitTotalDigits=0|| number2%10==0)
        {
            
        }
        */
        
        //need something here since it was giving numberformat exception.
        //most likely since lengthFirstNumber or lengthSecondNumber is
        //already 0
    
        lastDigitFirstNumber=number1.substring(lengthFirstNumber-1);
        System.out.println("last digit first number: " + lastDigitFirstNumber);
        
        
       
        lastDigitSecondNumber=number2.substring(lengthSecondNumber-1);
        System.out.println("last digit second number: " + lastDigitSecondNumber);
     
        System.out.println("MORE!!!");        //System.out.println(lastDigitFirstNumber);
        //System.out.println(lastDigitSecondNumber);
        
        //this has to be separate since it is possible for a remainder to be carried forward
        //from previous method call AND current method call having total less than 10 or greater than 10.
       
       try
       {
          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)
            //{
                
            //}
            }
    }
    catch (NumberFormatException e)
    {
        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
      try
      {
      if (lastDigitFirstNumber!=null)
      {
          total = Integer.valueOf(lastDigitSecondNumber) + firstDigitTotalDigits;
          System.out.println("1in here: " + total);
          System.out.println("1in here: " + lastDigitSecondNumber);
      }
      
      //at this point we have dealt with 
      
      
      }
      catch (NumberFormatException n)
      {
          
          total = Integer.valueOf(lastDigitFirstNumber) + firstDigitTotalDigits;
          System.out.println("2in here: " + total);
          System.out.println("2in here: " + lastDigitFirstNumber);
          
      }
      
      //this part needs addressing.
      grandTotal = Integer.toString(total) + grandTotal;
      System.out.println(grandTotal);
      //System.exit(0);
      
        
        
    }
        
        return "test";
        
        
    //}  // end of if  number%10!=0
    
    }
}