0

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.

7
  • what error do you get when there are two zeroes after each other? Commented Jun 25, 2015 at 19:53
  • @RamanShrivastava sorry, it's ok, just i can't do it with long numbers. (post edited) Commented Jun 25, 2015 at 19:58
  • Ok. What happens what you try with "long" numbers. Ideally it should work if your long number is well within the range of long supported by java. Commented Jun 25, 2015 at 19:59
  • Primitive types (like int or long) have a limit, they can't be as big as you want; I guess you need to use something more complicated using BigInteger's Commented Jun 25, 2015 at 20:00
  • Got it.. u mean "long long" not "long" .. Commented Jun 25, 2015 at 20:01

4 Answers 4

4

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');
Sign up to request clarification or add additional context in comments.

Comments

2

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
        }
    }

Comments

1
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]

1 Comment

There is java.math.BigInteger.TEN and BigInteger[] divideAndRemainder(BigInteger divisor).
0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.