I am not getting the correct output for this method. I have looked at the BigInteger class, and even tried to implement, after rewriting for my instance variable, but it did not carry the numbers over to the next digit, or bring down the remainders.

Thanks in advance.

public void Add (int[]a,int[]b){

           
            int sum = 0;
            Swap(a,b);// makes b[] the smaller array


            flip(a); // flips array
            flip(b); // 123 becomes 321

            int[] c = new int[a.length]; // variable to hold Answer....
            int diff = 0;

            for (int i = a.length-1; i >=0; i--){

                sum = a[i] + b[i] + diff;
                diff = 0;

                    if (sum > 9 && i > 0){
                        sum -= 10;
                        diff = 1;
                    }

                c[i] = sum;  
            }
            
                flip(c);
                print(c);

Output:
Enter First Number: 123456789
Enter Second Number: 123456
123456789 + 123456 = 0005791416

Enter First Number: 123456789
Enter Second Number: 123456789
123456789 + 123456789 = 2468035719

Enter First Number: 123456
Enter Second Number: 123456789
123456 + 123456789 = .....
(ArrayIndexOutOfBoundsException)

I am not getting the correct output for this method. I have looked at the BigInteger class, and even tried to implement, after rewriting for my instance variable, but it did not carry the numbers over to the next digit, or bring down the remainders.

Thanks in advance.

public void Add (int[]a,int[]b){

           
            int sum = 0;
            Swap(a,b);// makes b[] the smaller array


            flip(a); // flips array
            flip(b); // 123 becomes 321

            int[] c = new int[a.length]; // variable to hold Answer....
            int diff = 0;

            for (int i = a.length-1; i >=0; i--){

                sum = a[i] + b[i] + diff;
                diff = 0;

                    if (sum > 9 && i > 0){
                        sum -= 10;
                        diff = 1;
                    }

                c[i] = sum;  
            }
            
                flip(c);
                print(c);

Output:
Enter First Number: 123456789
Enter Second Number: 123456
123456789 + 123456 = 0005791416

Enter First Number: 123456789
Enter Second Number: 123456789
123456789 + 123456789 = 2468035719

Enter First Number: 123456
Enter Second Number: 123456789
123456 + 123456789 = .....
(ArrayIndexOutOfBoundsException)

This is what you need to do

public void Add (int[]a,int[]b)
{
        
            int sum = 0;
	    if(a.length<b.length)	
	         Swap(a,b);// makes b[] the smaller array

            

            flip(a); // filps array
            flip(b); // 123 becomes 321

            int[] c = new int[a.length];
	    int[] d = new int[a.length];
	    for(int i=0;i<b.length;i++)
			d[i]=b[i];
	    for(int i=b.length;i<a.length,i++)
			d[i]=0;
            int diff = 0;

            for (int i = a.length-1; i >=0; i--){

                sum = a[i] + d[i]+ diff;
                diff = 0;
                    if (sum > 9 && i > 0){
                        sum -= 10;
                        diff = 1;
                    }

                c[i] = sum;
                
            }
                flip(c);
                printArray(c);
}

I hope you will understand now what I meant in my last post

This is what you need to do

public void Add (int[]a,int[]b)
{
        
            int sum = 0;
	    if(a.length<b.length)	
	         Swap(a,b);// makes b[] the smaller array

            

            flip(a); // filps array
            flip(b); // 123 becomes 321

            int[] c = new int[a.length];
	    int[] d = new int[a.length];
	    for(int i=0;i<b.length;i++)
			d[i]=b[i];
	    for(int i=b.length;i<a.length,i++)
			d[i]=0;
            int diff = 0;

            for (int i = a.length-1; i >=0; i--){

                sum = a[i] + d[i]+ diff;
                diff = 0;
                    if (sum > 9 && i > 0){
                        sum -= 10;
                        diff = 1;
                    }

                c[i] = sum;
                
            }
                flip(c);
                printArray(c);
}

I hope you will understand now what I meant in my last post

It didnt work. My output now is:

Enter First Number: 123456789
Enter Second Number: 1
123456789 + 1 = 1234567810

Enter First Number: 1
Enter Second Number: 123456789
1 + 123456789 = java.lang.ArrayIndexOutOfBoundsException: 1

It didnt work. My output now is:

Enter First Number: 123456789
Enter Second Number: 1
123456789 + 1 = 1234567810

Enter First Number: 1
Enter Second Number: 123456789
1 + 123456789 = java.lang.ArrayIndexOutOfBoundsException: 1

Ok I got the mistake for 1st part
The code should be

public void Add (int[]a,int[]b)
{

            int sum = 0;
	    if(a.length<b.length)
	         Swap(a,b);// makes b[] the smaller array
          
            flip(a); // filps array
            flip(b); // 123 becomes 321
            

            int[] c = new int[a.length];
	    int[] d = new int[a.length];
	    for(int i=0;i<b.length;i++)
			d[i]=b[i];
	    for(int i=b.length;i<a.length;i++)
			d[i]=0;
            int diff = 0;

            for (int i = 0; i<a.length; i++){

                sum = a[i] + d[i]+ diff;
                diff = 0;
                    if (sum > 9 && i <=a.length-2){
                        sum -= 10;
                        diff = 1;
                    }

                c[i] = sum;

            }
                flip(c);
                printArray(c);
}

This will print the correct result. For second part, i.e. ArrayIndexOutofBoundException, I think you have a problem in Swap method. Kindly check your Swap method that it swaps the two array if the length of array1<array2.

commented: Everything Offered has been very helpful. +1

Ok I got the mistake for 1st part
The code should be

if (sum > 9 && i <=a.length-2){
                        sum -= 10;
                        diff = 1;
                    }

This will print the correct result. For second part, i.e. ArrayIndexOutofBoundException, I think you have a problem in Swap method. Kindly check your Swap method that it swaps the two array if the length of array1<array2.

I fixed the swap method, but even tho what you had correct the additional number, it doesn't carry the one to the next number.

Output:

Enter First Number: 99999
Enter Second Number: 1
99999 + 1 = 99990

Enter First Number: 9
Enter Second Number: 11111
9 + 11111 = 11110

I fixed the swap method, but even tho what you had correct the additional number, it doesn't carry the one to the next number.

Output:

Enter First Number: 99999
Enter Second Number: 1
99999 + 1 = 99990

Enter First Number: 9
Enter Second Number: 11111
9 + 11111 = 11110

Well it runs perfectly on my system. The carrry part is happening. Please post your complete code so that I can look what is happening

Well it runs perfectly on my system. The carrry part is happening. Please post your complete code so that I can look what is happening

package pkg;
/**=============================================
Big Add
================================================
=============================================**/

//  ======
//  Import
//  ===================
    import java.util.*;

    public class Main {


        public static void main (String [] args){

            int   sel;
            char  goAgain;
            
            BigInteger user = new BigInteger();
            Scanner keyboard = new Scanner(System.in);

            do{



                System.out.println("================="
                                   +"=======================");
                System.out.println("Please Select which"
                                    + " Operation to Preform");
                System.out.print(  "   Add(1)    Multiply (2)   Ans: ");
                
                sel = keyboard.nextInt();

                if( sel == 1){

                     user.Input();

                }
                
                else if(sel == 2){

                    System.out.println("Under Construction");
                    
                }

                else{
                    System.out.println("Incorrect Selection!"+ '\n'
                                       +"Please Try Again." );
                }

            System.out.println();
            System.out.print("Would you like to" +
                             " run again? (y/n) Ans: ");
  
            String s9 = keyboard.next();
            goAgain   = s9.charAt(0);
            System.out.println();
            }while (goAgain == 'y');



        }//Method Main


    }//Class Main
import java.util.Scanner;


    public class BigInteger {
    


        private int[]  NumC;
        private int[]  NumD;
        private int[] result;  

        public BigInteger(){

            NumC   = null;
            NumD   = null;
            result = null;

        }//Assessor


        public void Input(){

            Scanner keyboard = new Scanner(System.in);
            String aa;
            String bb;
            System.out.println();
            System.out.print("Enter First Number: ");
            aa = keyboard.nextLine();
            NumC = convertArray(aa);
            setNumC(NumC);


            System.out.print("Enter Second Number: ");
            bb = keyboard.nextLine();
            NumD = convertArray(bb);
            setNumD(NumD);


            System.out.println();
            System.out.print("   ");
            print(NumC);
            System.out.print(" + ");
            print(NumD);
            System.out.print(" = ");
            Add(NumC,NumD);
            



        }// Input

        
        public BigInteger(int[] a,
                          int[] b,
                          int[] c){

            this.NumC   = a;
            this.NumD   = b;
            this.result = c;

        }//Constructor

        public void setNumC (int[] a){

            NumC = a;
        

        }//Mutator NumC


        public void setNumD (int [] b){

            NumD = b;


        }//Mutator NumD

        public void setResult (int[] c){

            result = c;

        }//Mutator NumC

        public static int[] convertArray(String b){

            int[]arr = new int[b.length()];
            arr[0] = 0;
                for (int i = 0; i < b.length(); i++){
               
                arr[i] = Character.digit(b.charAt(i),10);
                
		}

            return arr;

            
          }//end of toArray

        public static void flipArray(int[] b) {
            int left  = 0;         
            int right = b.length-1;
  
            while (left < right) {
    
                int temp = b[left]; 
                b[left]  = b[right]; 
                b[right] = temp;
     
                left++;
                right--;
            }
        }//FilpArray

        public void Add (int[]a,int[]b){

            int sum = 0;
            Swap(a,b);// makes b[] the smaller array


            flipArray(a); // filps array
            flipArray(b); // 123 becomes 321
            int[] c = new int[a.length];
            int diff = 0;
            
            for (int i = b.length-1; i >=0; i--){

                sum = a[i] + b[i]+ diff;
                diff = 0;
          
                    if (sum > 9 && i > 0){
                        sum = sum - 10;
                        diff = 1;
                    }
                c[x] = sum;


            }
            flipArray(c);
            print(c);

        }//Add method

        public int[] getSumC(){


           return NumC;

        }//Assessor

        public int[] getSumD(){


           return NumD;

        }//Assessor

        public int[] getResult (){

            return result;

        }//Assessor

        public static void print(int[] b){

            int i = 0;

            for(i = 0;i < b.length;i++){
                
                System.out.print(b[i]);
            }
            
        }//Print
  
        public void Swap (int[]a,int[]b){
    
        int[] temp;
        
        if(a.length < b.length){
                temp = b;
                b = a;
                a = temp;
                
            }
        }//Swap

        @Override
        public String toString(){

            if(NumC == null){

                print(NumC);
            }
            else if(NumD == null){

                print(NumD);
            }
           
            return ("error");

        }//toString

    }//End Class

My coding isn't perfect, I hope you can read it all.
I really appreciate all the help!

I have made a few change in your BigInteger class.
Probably this might not be the most efficient way, but its working perfectly fine. I will try to make it better on weekend

package pkg;



import java.util.Scanner;


    public class BigInteger {



        private int[]  NumC;
        private int[]  NumD;
        private int[] result;

        public BigInteger(){

            NumC   = null;
            NumD   = null;
            result = null;

        }//Assessor


        public void Input(){

            Scanner keyboard = new Scanner(System.in);
            String aa;
            String bb;
            System.out.println();
            System.out.print("Enter First Number: ");
            aa = keyboard.nextLine();
            NumC = convertArray(aa);
            setNumC(NumC);


            System.out.print("Enter Second Number: ");
            bb = keyboard.nextLine();
            NumD = convertArray(bb);
            setNumD(NumD);


            System.out.println();
            System.out.print("   ");
            print(NumC);
            System.out.print(" + ");
            print(NumD);
            System.out.print(" = ");
            Add(NumC,NumD);




        }// Input


        public BigInteger(int[] a,
                          int[] b,
                          int[] c){

            this.NumC   = a;
            this.NumD   = b;
            this.result = c;

        }//Constructor

        public void setNumC (int[] a){

            NumC = a;


        }//Mutator NumC


        public void setNumD (int [] b){

            NumD = b;


        }//Mutator NumD

        public void setResult (int[] c){

            result = c;

        }//Mutator NumC

        public static int[] convertArray(String b){

            int[]arr = new int[b.length()];
            
                for (int i = 0; i < b.length(); i++){

                arr[i] = Character.digit(b.charAt(i),10);

		}

            return arr;


          }//end of toArray

        public static void flipArray(int[] b) {
            int left  = 0;
            int right = b.length-1;

            while (left < right) {

                int temp = b[left];
                b[left]  = b[right];
                b[right] = temp;

                left++;
                right--;
            }
        }//FilpArray

        public void Add (int[]a,int[]b){

            int sum = 0;
            //Swap(a,b);// makes b[] the smaller array
            flipArray(a); // filps array
            flipArray(b); // 123 becomes 321
            int[] arr1,arr2;
            if(a.length<b.length)
            {
                arr1 = new int[b.length];
                for(int i=0;i<a.length;i++)
                    arr1[i]=a[i];
                for(int i=a.length;i<b.length;i++)
                    arr1[i]=0;
                arr2=b;
            }
            else
            {
                arr1 = new int[a.length];
                for(int i=0;i<b.length;i++)
                    arr1[i]=b[i];
                for(int i=b.length;i<a.length;i++)
                    arr1[i]=0;
                arr2=a;
            }
            int[] c = new int[arr1.length];
            int diff = 0;
            for (int i = 0; i<arr1.length; i++){

                sum = arr1[i] + arr2[i]+ diff;
                diff = 0;
                    if (sum > 9 && i <=arr1.length-2){
                        sum -= 10;
                        diff = 1;
                    }

                c[i] = sum;

            }
            flipArray(c);
            print(c);
            

        }//Add method

        public int[] getSumC(){


           return NumC;

        }//Assessor

        public int[] getSumD(){


           return NumD;

        }//Assessor

        public int[] getResult (){

            return result;

        }//Assessor

        public static void print(int[] b){

            int i = 0;

            for(i = 0;i < b.length;i++){

                System.out.print(b[i]);
            }

        }//Print

        /*public void Swap (int[]a,int[]b){

        int[] temp;

        if(a.length < b.length){
                temp = b;
                b = a;
                a = temp;

            }
        }//Swap*/

        @Override
        public String toString(){

            if(NumC == null){

                print(NumC);
            }
            else if(NumD == null){

                print(NumD);
            }

            return ("error");

        }//toString

    }//End Class

I have removed your Swap method...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.