This site requires JavaScript, please enable it in your browser!
Greenfoot back
Super_Hippo
Super_Hippo wrote ...

2017/6/8

How to convert an int into an array containing its digits?

Super_Hippo Super_Hippo

2017/6/8

#
I want to divide an integer into a set of its digits. So for example an input (=score) of 123456 should result in {1,2,3,4,5,6} (=s). Right now I have this and it works, but is there a better way?
        int a = score / 1000000000; score -= a * 1000000000;
        int b = score / 100000000; score -= b * 100000000;
        int c = score / 10000000; score -= c * 10000000;
        int d = score / 1000000; score -= d * 1000000;
        int e = score / 100000; score -= e * 100000;
        int f = score / 10000; score -= f * 10000;
        int g = score / 1000; score -= g * 1000;
        int h = score / 100; score -= h * 100;
        int i = score / 10; score -= i * 10;
        int j = score;
        
        int num = 1;
        if (a>0) num = 10;
        else if (b>0) num = 9;
        else if (c>0) num = 8;
        else if (d>0) num = 7;
        else if (e>0) num = 6;
        else if (f>0) num = 5;
        else if (g>0) num = 4;
        else if (h>0) num = 3;
        else if (i>0) num = 2;
        
        int[] s = null;
        switch (num)
        {
            case 1: s = new int[] {j}; break;
            case 2: s = new int[] {i, j}; break;
            case 3: s = new int[] {h, i, j}; break;
            case 4: s = new int[] {g, h, i, j}; break;
            case 5: s = new int[] {f, g, h, i, j}; break;
            case 6: s = new int[] {e, f, g, h, i, j}; break;
            case 7: s = new int[] {d, e, f, g, h, i, j}; break;
            case 8: s = new int[] {c, d, e, f, g, h, i, j}; break;
            case 9: s = new int[] {b, c, d, e, f, g, h, i, j}; break;
            case 10: s = new int[] {a, b, c, d, e, f, g, h, i, j}; break;
        }
danpost danpost

2017/6/8

#
I will presume that all int values will be non-negative since an int array cannot contain a minus sign character. One possible way is this:
int num = 123456;
String s = ""+num;
int[] numArray = new int[s.length()];
for (int i=0; i<numArray.length; i++) numArray[i] = s.charAt(i)-48;
Another could be:
int num = 123456;
int[] numArray = new int[(""+num).length()];
for (int i=numArray.length-1; i>= 0; i--)
{
    numArray[i] = num%10;
    num = num/10;
}
As an alternative to getting the number of digits, you could use:
int[] numArray = new int[1+(int)Math.log10(num)];
(but, I think a zero value will cause problems with this one)
Super_Hippo Super_Hippo

2017/6/8

#
Yes, every input will be positive (or 0). I found this line to get the number of digits:
n<1E5?n<1E2?n<1E1?1:2:n<1E3?3:n<1E4?4:5:n<1E7?n<1E6?6:7:n<1Eā€Œā€‹8?8:n<1E9?9:10
However, it was only a small part of improvement. It's amazing how the three lines can replace all that! Thank you very much! Just one small question: where is the -48 coming from? Ah, I just found the answer. Never mind :)
danpost danpost

2017/6/8

#
Super_Hippo wrote...
where is the -48 coming from?
The characters '0' through '9' are located at 48 through 57 in the character code system. So, for example, the character '1' has a value of 49, or: (char)49 = '1' or (int)'1' = 49
Super_Hippo Super_Hippo

2017/6/8

#
Quite interesting is that the following will always result in two same digits as the output in each line. (Yes, I changed the variable names because I needed it to be that s later.)
String numString = ""+n;
int[] s = new int[numString.length()];
for (int i=0; i<s.length; i++)
{
    s[i] = numString.charAt(i)-48;
    System.out.println(numString.charAt(i) + " " + (numString.charAt(i)-48));
}
So it looks like charAt(i) = charAt(i) - 48 But if I remove the -48 from line 5, I get the expected ArrayIndexOutOfBoundsException.
danpost danpost

2017/6/8

#
The first part is using the character, the second is converting an int value to a string. So, yes: ""+(char)48 = ""+((int)0) or ""+charAt(i) = ""+(((int)charAt(i))-48)
Super_Hippo Super_Hippo

2017/6/8

#
Ah ok right, that just confused me a little bit. Tested it with using
(int) numString.charAt(i)
for the first part and that prints 48 for 0 now. Thanks for clearing that up.
danpost danpost

2017/6/8

#
There is also this:
int num = 123456;
char[] cs = (""+num).toCharArray();
int[] s = new int[cs.length];
for (int i=0; i<cs.length; i++) s[i] = cs[i]-48;
which looks a little cleaner. And also this:
int num = 123456;
String numString = ""+num;
int[] s = new int[numString.length()];
for (int i=0; i<s.length; i++) s[i] = "0123456789".indexOf(numString.charAt(i));
danpost danpost

2017/6/9

#
Super_Hippo wrote...
I found this line to get the number of digits:
n<1E5?n<1E2?n<1E1?1:2:n<1E3?3:n<1E4?4:5:n<1E7?n<1E6?6:7:n<1Eā€Œā€‹8?8:n<1E9?9:10
However, it was only a small part of improvement.
That line seems to be too complicated to easily understand. This would work, however:
int digitCount= score == 0 ? 1 : 1+(int)Math.log10(score);
davmac davmac

2017/6/9

#
Just one small question: where is the -48 coming from?
Incidentally, you can replace -48 with -'0' and it works well, as well as being perhaps a little less cryptic:
for (int i=0; i<numArray.length; i++) numArray[i] = s.charAt(i)-'0';
You need to login to post a reply.