Moss
Moss

Reputation: 107

divide every number of long number in java

i am newbie in android developing, i have a simple question.

Imagine I have a long long number, like 166516516516516515.

And i want to have divided output like: 1,6,6,5,1,6,5,1,6,5,1,6,5,1,6,5,...

I mean i want to have every every one in output.

I wrote this algorithm :

int temp = 2536;
ArrayList<Integer> array = new ArrayList<Integer>();
do {
    array.add(temp % 10);
    temp /= 10;
}
while (temp > 0);
for (int i = 0; i < array.size(); i++) {
    Log.i("LOG", "Dynamic Numbers Array Index #" + i + " = " + array.get(i));
}

it works for small numbers (int)

but for long number it doesn't give true work,

How can i solve it to work with big numbers?

thanks.

Upvotes: 0

Views: 809

Answers (4)

Pavel
Pavel

Reputation: 1

Just read that stuff into a string and do:

for(char c : str.toCharArray()){}

No need to divide anything and you can have arbitrary length.

If you need ints just convert by doing:

int i = (int)(c - '0');

Upvotes: 4

Rajesh
Rajesh

Reputation: 2155

split longString to intArray

Split longString to char array and then use Character.digit to get digit value.

public static int[] splitLong(String longStr) {

    int i = 0;
    int[] nums = new int[longStr.length()];     

    for (char l : longStr.toCharArray())
        nums[i++] = Character.digit(l, 10);     

    return nums;
}

Other approach:

public static int[] splitLongNum(String longStr) {

    int len = longStr.length();
    int[] nums = new int[len];
    for (int j = 0; j < len; j++)
        nums[j] = Character.digit(longStr.charAt(j), 10);
    return nums;
}

Upvotes: 0

ASP
ASP

Reputation: 447

import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class Callone {


    public static void main(String[] args)
    {
        BigInteger i = new BigInteger("166516516516516515");
        List<Integer> list = new ArrayList<Integer>();
        BigInteger ten = new BigInteger("10");
        while (!i.equals(BigInteger.ZERO))
        {
            list.add(0, i.mod(ten).intValue());
            i = i.divide(ten);
        }

        System.out.println(list.toString());


    }
}

output: [1, 6, 6, 5, 1, 6, 5, 1, 6, 5, 1, 6, 5, 1, 6, 5, 1, 5]

Upvotes: 1

poss
poss

Reputation: 1789

First of all, you need to watch out if you can "cram" all your number into simple int. Chances are that if it's too long you simply cannot do that at all - as you probably noticed by now.

I took another approach to the solution, but it might not be exactly what you need. Treat the number as a string.

String temp = "166516516516516515";
breakUp(temp);

private static void breakUp(String string){
        int length = string.length();

        for (int i = 0; i < length; i++) {
            String temp = string.substring(i, i+1);
            int tempInt = Integer.valueOf(temp);
            System.out.print(tempInt + " - "); //or whatever here, you can make function return list instead of void
        }
    }

Upvotes: 2

Related Questions