1

I am currently having an issue regarding combining a integer array into an integer.

I looked into a couple of other ways to do them at Way to combine integer array to a single integer variable?, but I still do not understand why I am running into errors.

My goal is to turn:

[6, 2, 3, 3, 8, 7, 7, 7, 0, 1, 6]

into

62338777016

It currently works when given smaller integer arrays such as:

[1, 3, 4, 4]
-> 1344

It starts breaking down once the number of elements reaches 10. Does anybody have a possible solution?

Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44

6 Answers6

0

We need to see the Error message to be sure but my first guess will be that you reach the size limit of int (2,147,483,647).

yosahr
  • 14
  • 1
  • [6, 2, 3, 3, 8, 7, 7, 7, 0, 1, 6] 2147483647 [2, 3, 3, 8, 7, 7, 7, 0, 1, 6, 5] 2147483647 [3, 3, 8, 7, 7, 7, 0, 1, 6, 5, 3] 2147483647 [3, 8, 7, 7, 7, 0, 1, 6, 5, 3, 8] 2147483647 [8, 7, 7, 7, 0, 1, 6, 5, 3, 8, 8] 2147483647 [7, 7, 7, 0, 1, 6, 5, 3, 8, 8, 7] 2147483647 [7, 7, 0, 1, 6, 5, 3, 8, 8, 7, 3] 2147483647 [7, 0, 1, 6, 5, 3, 8, 8, 7, 3, 4] 2147483647 – chimmichanngas Dec 10 '18 at 06:22
  • The array is the integer array and the number next to it is the combined element number – chimmichanngas Dec 10 '18 at 06:23
  • You can use long or BigInteger but more important is that you check the size of the array and validate that you are not overflowing – yosahr Dec 10 '18 at 06:44
0

You are overflowing the integer's maximum size of 2147483647. One way to address this is to use a BigInteger instead of an int:

BigInteger bigInt = BigInteger.ZERO;
for (int i : ints) {
    bigInt = bigInt.multiply(BigInteger.TEN).add(BigInteger.valueOf(i));
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Well one possible way to solve this, I am assuming all integers are positive.

You can concatenate all integer array values into a String and form a single string.

So, [6, 2, 3, 3, 8, 7, 7, 7, 0, 1, 6] becomes 62338777016 (String).

BigInteger has a constructor (https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#BigInteger(java.lang.String))

That you can leverage, to get a BigInteger representation of the value.

Himanshu Bhardwaj
  • 4,038
  • 3
  • 17
  • 36
0

You can perform it like this:

Integer[] arr = {6, 2, 3, 3, 8, 7, 7, 7, 0, 1, 6};
Long val = Long.valueOf(Arrays.stream(arr).map(String::valueOf).collect(Collectors.joining("")));
Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44
Burak Akyıldız
  • 1,550
  • 15
  • 25
0

Here you are trying to use a integer for more than 10 digits which exceeds maximum value of 2,147,483,647, so you could use the below code which make minor changes like using a double.

  Integer[] arr = new Integer[] { 6, 2, 3, 3, 8, 7, 7, 7, 0, 1, 6 };
    Double myData = 0d;
    for (int i = 0; i < arr.length; i++) {
        double productfactor = (Math.pow(10, (arr.length-1-i)));
        myData = myData+arr[i]*productfactor;
    }
    String formatted = new BigDecimal(Double.valueOf(myData)).toString();
0
public static long convert(int[] arr) {
    long res = 0;

    for (int digit : arr) {
        // negative value is marker of long overflow
        if (digit < 0 || res * 10 + digit < 0)
            throw new NumberFormatException();
        res = res * 10 + digit;
    }

    return res;
}

This is not an universal method, because of Long.MAX_VALUE. Otherwize, you have to use BigInteger isntead of long.

public static BigInteger convert(int[] arr) {
    // reserve required space for internal array
    StringBuilder buf = new StringBuilder(arr.length);

    for (int digit : arr)
        buf.append(digit);

    // create it only once
    return new BigInteger(buf.toString());
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35