How to add two integer array

Hello guys, here is my code so far. This is Java. I already converted string into an integer. My only problem is, how you add two arrays?


import java.util.*;

public class lab3
{
public static void main (String [] args)
{


Scanner in = new Scanner (System.in);

int count;
int num1 = 0;
int num2 = 0;


String integer1;
String integer2;


System.out.print("Enter a positive integers: ");
integer1 = in.nextLine();

System.out.print("Enter another positive integers: ");
integer2 = in.nextLine();

System.out.println(" ");

int [] convert = new int [integer1.length()];
int [] convert2 = new int [integer2.length()];


for (count = 0; count < integer1.length(); count ++)
{

convert [count] = Character.getNumericValue(integer1.charAt(count));
System.out.print(+convert[count]);

}

System.out.println(" ");


for (count = 0; count < integer2.length(); count ++)
{

convert2 [count] = Character.getNumericValue(integer2.charAt(count));
System.out.print(+convert2[count]);

}


System.out.println();
System.out.println("---------");






}
}
closed account (10oTURfi)
public static void main (String [] args)
Thats odd syntax to see on this forum. Mainly beacuse this is cplusplus.com :)
closed account (zb0S216C)
I think you're on the wrong forum, mate. This forum is centralized around C/C++, not Java.

In C++, adding two arrays together is an easy task, assuming the arrays have the same length, of course.

1
2
3
4
5
6
7
8
const int array_length( 10 );

int array_a[array_length] = { 0 };
int array_b[array_length] = { 1 };

// Add a to b:
for( int i( 0 ); i < array_length; ++i )
    array_a[i] += array_b[i];


Wazzak
Last edited on
Okay. But what if the array has different length?
closed account (zb0S216C)
Then you'll have to pass the length of both arrays to the function. Although, std::vector is more preferred; they are the modern array.

Wazzak
Topic archived. No new replies allowed.