
-----------------------------------
CooKieLord
Fri Oct 12, 2007 7:08 pm

Determining the amount of digits, and storing the values in array
-----------------------------------
Situation: I want to break down an integer number into a bunch of digits, and store the value in an array. 
E.g. The user enters 1 337.

The program will break it down to 1, 3, 3, 7 and store each digit in an array such that:
integerArray
public class test {
   public static void main  (String

-----------------------------------
HeavenAgain
Fri Oct 12, 2007 7:14 pm

RE:Determining the amount of digits, and storing the values in array
-----------------------------------
try making the number into a string first, then its automatically in an array
just have to convert

-----------------------------------
Euphoracle
Fri Oct 12, 2007 7:19 pm

RE:Determining the amount of digits, and storing the values in array
-----------------------------------
Well, you could do it that way, or you could do it the easy way and use a string.


public clas test
{
	public static void main(String

Of course, if you're not allowed to do that, this won't help much :|  Sorry for any syntax errors if any, I only have access to notepad at the moment and no JDK.

-----------------------------------
richcash
Fri Oct 12, 2007 7:44 pm

Re: Determining the amount of digits, and storing the values in array
-----------------------------------
If you take the log (base 10) of a number and round down, the result will be 1 less than the number of digits in that number.

Remember, Math.log() in java.lang.Math is the natural log, so you have to use the base changing formula :
Math.log(num)/Math.log(10)will give you the log (base 10) of a number.

Do you know how to get the number of digits now? (Note, you'll have to make adjustments for 0 and negative numbers if you choose to include them).

As you said, you can use division and modulus once you know the number of digits.

-----------------------------------
richcash
Sat Oct 13, 2007 12:08 pm

Re: Determining the amount of digits, and storing the values in array
-----------------------------------
In case you're having trouble :
int num = 9846711;
int numOfDigits = (int) (Math.log (num) / Math.log(10)) + 1;

Post if you need help with storing the individual digits.

-----------------------------------
wtd
Sat Oct 13, 2007 4:05 pm

RE:Determining the amount of digits, and storing the values in array
-----------------------------------
class MySpecialInteger {
    private int value;

    public MySpecialInteger() {
        this(0);
    }

    public MySpecialInteger(int value) {
        this.value = value;
    }

    public int intValue() {
        return value;
    }

    public int[] digits() {
        Integer[] temp = digitsHelper(value, new java.util.ArrayDeque()); 
        int index = 0;
        int[] digits = new int[temp.length];
        
        for (Integer v : temp) {
			digits[index++] = v;
        }
        
        return digits;
    }

    private static Integer[] digitsHelper(
      int value, 
      java.util.ArrayDeque accumulator) {
        if (value > 0) {
            accumulator.addFirst(value % 10);

            return digitsHelper(value / 10, accumulator);
        }
        else {
            return (Integer[])accumulator.toArray();
        }
    }
}

Recursion FTW!

-----------------------------------
richcash
Sat Oct 13, 2007 5:21 pm

Re: Determining the amount of digits, and storing the values in array
-----------------------------------
I would rather write your digits() method like this.
public int

But I guess thinking recursively about it is cool too.

-----------------------------------
wtd
Sat Oct 13, 2007 6:03 pm

RE:Determining the amount of digits, and storing the values in array
-----------------------------------
You will also notice that my digitsHelper method is tail-recursive.

-----------------------------------
CooKieLord
Tue Oct 16, 2007 2:18 pm

RE:Determining the amount of digits, and storing the values in array
-----------------------------------
Thank for all the help guys, and sorry I'm a little late in replying.

I'm mainly looking at the way richcash showed me. It's simpler for me, because I don't quite understand how to get recursion to work yet.

Now, the only problem I have is storing the broken down values in the array. With the way you're shown to me, it would place the units in [0], the 10s in [1], the 100s in [2], etc. I need the highest digit, let's say 1000s in [0], the 100s in [1], the 10s in [2] and the 1s in [3] 

(I'm talking about array subscripts right? or is it called index?)

Don't tell me how to do it. I'll edit this post if I can't figure it out. I just wanted to let you guys know that I read the posts. Thanks again!

-----------------------------------
Euphoracle
Tue Oct 16, 2007 2:26 pm

RE:Determining the amount of digits, and storing the values in array
-----------------------------------
Reverse the array:

int

-----------------------------------
CooKieLord
Tue Oct 16, 2007 2:36 pm

RE:Determining the amount of digits, and storing the values in array
-----------------------------------
Well this is what I did


public class test {
   public static void main  (String

I had two variables, one for the array and the other for the modulo. Is that what you meant by "reversing"?

-----------------------------------
Euphoracle
Tue Oct 16, 2007 9:34 pm

RE:Determining the amount of digits, and storing the values in array
-----------------------------------
Your method does work, yes.
