How to add the numbers stored in the two string,?

suppose String S1 = "19238248931244327089";
S2 = "43109431029897431"; the value of the number required to obtain s1+s2
cannot be added using the class library.

Mar.28,2021

1, use BigInteger class, BigDecimal class

public static void main(String[] args) {
        String a="19238248931244327089";
        String b="43109431029897431";
        String str=new BigInteger(a).add(new BigInteger(b)).toString();
        System.out.println(str);
    }

2, reverse string, align string vacancy 0, add two positive integers

public static String add(String n1,String n2){

            String result="";

            //
            String num1=new StringBuffer(n1).reverse().toString();
            String num2=new StringBuffer(n2).reverse().toString();

            int len1=num1.length();
            int len2=num2.length();
            int maxLen=len1>len2?len1:len2;
            //()
            int nSum[]=new int[maxLen+1];

            boolean nOverFlow=false;

            //
            if(len1<len2){
                for (int i = len1; i < len2; iPP) {
                    num1+="0";
                }
            }else if(len1>len2){
                for (int i = len2; i < len1; iPP) {
                    num2+="0";
                }
            }

            //
            for (int i = 0; i < maxLen; iPP) {
                //
                if (nOverFlow) {
                    nSum[i]=Integer.parseInt(num1.charAt(i)+"")+
                            Integer.parseInt(num2.charAt(i)+"")+1;
                }else{
                    nSum[i]=Integer.parseInt(num1.charAt(i)+"")+
                            Integer.parseInt(num2.charAt(i)+"");
                }
                //
                nOverFlow=handleSumOverTen(nSum,i);

            }

            //
            if(nOverFlow) {
                nSum[maxLen] = 1;
            }else {
                nSum[maxLen] =0 ;
            }

            for (int i = 0; i < nSum.length; iPP) {
                result+=String.valueOf(nSum[i]);
            }
            String result1=new StringBuffer(result).reverse().toString();
            return result1;
        }

        private static boolean handleSumOverTen(int[] nSum, int i) {

            boolean flag = false;
            if(nSum[i] >= 10) {
                nSum[i] = nSum[i] - 10;
                flag = true;
            }
            else {
                flag = false;
            }
            return flag;
        }
       public static void main(String[] args) {
        String num=add("19238248931244327089", "43109431029897431");
        System.out.println(num);
        }

this result may have an extra 0 characters

3, complete the string (use the insert method in StringBuffere to insert len zeros where the string index is 0), align and add

public class test {

    public static void main(String[] args) {
        int[] result = bigNumSum("19238248931244327089", "43109431029897431");
        for(int i=0; i < result.length; iPP) {
            System.out.print(result[i]);
        }
    }

    public static int[] bigNumSum(String num1, String num2) {

        String number1 = num1;
        String number2 = num2;

        int len1=number1.length();
        int len2=number2.length();
        int len=Math.abs(len1-len2);
        char insertNum[]=new char[len];
        for (int i = 0; i < insertNum.length; iPP) {
            insertNum[i]='0';
        }
        String str1="";
        String str2="";
        //
        if (len1<len2) {

            str1=new StringBuffer(number1).insert(0, insertNum).toString();
            str2=number2;
        }else if(len1>len2){
            str1=number1;
            str2=new StringBuffer(number2).insert(0, insertNum).toString();
        }

        //
        char[] ch1 = str1.toCharArray();
        char[] ch2 = str2.toCharArray();
        int[] sum;
        //true>=10
        boolean flag = false;

        //+1>10
        sum = new int[ch1.length+1];
        //
        for(int i=ch1.length-1; i>=0; i--) {
            //11
            if(flag) {
                //
                sum[i+1] = (int)(ch1[i] - '0') + (int)(ch2[i] - '0') + 1;
            }else {
                sum[i+1] = (int)(ch1[i] - '0') + (int)(ch2[i] - '0');
            }
            flag = handleSumOverTen(sum, i); //>10
        }

        handleTopDigit(flag, sum); //
        return sum;


    }

    /*
     * >10
     */
    public static boolean handleSumOverTen(int[] sum, int i) {
        boolean flag = false;
        if(sum[i+1] >= 10) {
            sum[i+1] = sum[i+1] - 10;
            flag = true;
        }
        else {
            flag = false;
        }
        return flag;
    }

    /*
     * 
     */
    public static void handleTopDigit(Boolean flag, int[] sum) {
        if(flag) {
            sum[0] = 1;
        }else {
            sum[0] = 0;
        }
    }

}

4. This method is basically consistent with the three methods, but the difference is that the three methods do not need to judge step by step because the lengths of two strings are equal by filling 0, but the overall running efficiency is still high

.
public class test {

    public static void main(String[] args) {
        int[] result = bigNumSum("19238248931244327089", "43109431029897431");
        for(int i=0; i < result.length; iPP) {
            System.out.print(result[i]);
        }
    }

    public static int[] bigNumSum(String num1, String num2) {

        String number1 = num1;
        String number2 = num2;
        //
        char[] ch1 = number1.toCharArray();
        char[] ch2 = number2.toCharArray();
        int[] sum;
        //
        int len = Math.abs(ch1.length - ch2.length);
        //true>=10
        boolean flag = false;

        //
        if(ch1.length == ch2.length) {

            //+1>10
            sum = new int[ch1.length+1];
            //
            for(int i=ch1.length-1; i>=0; i--) {
                //11
                if(flag) {
                    //
                    sum[i+1] = (int)(ch1[i] - '0') + (int)(ch2[i] - '0') + 1;
                }else {
                    sum[i+1] = (int)(ch1[i] - '0') + (int)(ch2[i] - '0');
                }
                flag = handleSumOverTen(sum, i, len); //>10
            }

            handleTopDigit(flag, sum); //
            return sum;
        }
        else if(ch1.length > ch2.length) { //12
            sum = new int[ch1.length+1]; //1+1

            for(int i=ch2.length-1; i>=0; i--) {
                if(flag) {
                    sum[i+len+1] = (int)(ch1[i+len] - '0') + (int)(ch2[i] - '0') + 1;
                }
                else {
                    sum[i+len+1] = (int)(ch1[i+len] - '0') + (int)(ch2[i] - '0');
                }

                flag = handleSumOverTen(sum, i, len);
            }

            for(int i=ch1.length-ch2.length-1; i>=0; i--) { //1
                if(flag) {
                    sum[i+1] = (int)(ch1[i] - '0') + 1;
                }
                else {
                    sum[i+1] = (int)(ch1[i] - '0');
                }
                flag = handleSumOverTen(sum, i, 0);
            }

            handleTopDigit(flag, sum);
            return sum;
        }
        else {
            sum = new int[ch2.length+1];

            for(int i=ch1.length-1; i>=0; i--) {
                if(flag) {
                    sum[i+len+1] = (int)(ch1[i] - '0') + (int)(ch2[i+len] - '0') + 1;
                }
                else {
                    sum[i+len+1] = (int)(ch1[i] - '0') + (int)(ch2[i+len] - '0');
                }

                flag = handleSumOverTen(sum, i, len);
            }

            for(int i=ch2.length-ch1.length-1; i>=0; i--) {
                if(flag) {
                    sum[i+1] = (int)(ch2[i] - '0') + 1;
                }
                else {
                    sum[i+1] = (int)(ch2[i] - '0');
                }
                flag = handleSumOverTen(sum, i, 0);
            }

            handleTopDigit(flag, sum);
            return sum;
        }
    }

    /*
     * >10
     */
    public static boolean handleSumOverTen(int[] sum, int i, int len) {
        boolean flag = false;
        if(sum[i+len+1] >= 10) {
            sum[i+len+1] = sum[i+len+1] - 10;
            flag = true;
        }
        else {
            flag = false;
        }
        return flag;
    }

    /*
     * 
     */
    public static void handleTopDigit(Boolean flag, int[] sum) {
        if(flag) {
            sum[0] = 1;
        }else {
            sum[0] = 0;
        }
    }

}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e48b9c-44ce7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e48b9c-44ce7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?