0

I was wondering what I'm doing wrong, I tried to split the digits of an int and enter them into an array in Java. I don't know whats wrong, am I missing something when dealing with the array?

The array is declared like so:

    int[] digits = new int [100];

Here is the logic:

    while (r > 0) {
        // puts digits of n into array
        count = 0;
       sum = 0;
        while (n != 0) {
            digits[count] = n % 10;
            count++;
            n = n / 10;

        }

        // squares digits[] and populates into sum
        for (i = 0; i < count; i++) {
            sum = sum + digits[i]*digits[i];
        }

        // if sum is 1 then print 'Number is a happy number'
        if (sum == 1) {                
            addItems(b);
            r = 0;
        } else {
            r--;
        }
    }

Any help would be very much appreciated as I've been looking through the web for a few days now and haven't found any real help

11
  • 5
    You need to post more of the code. I just did ideone.com/jJcCG based on your code, and it works just fine... Commented Dec 21, 2011 at 22:13
  • 2
    What actual problem are you facing, this looks like it should work... Although, I wouldn't expect you to be able to get more than 9 or 10 digits out of an int, so your array is oversized. Commented Dec 21, 2011 at 22:14
  • Hey, I've edited my question, the code I've given you is to find if the number the user has entered is happy, I know this code works becsause I made it in C, but there must be something I need to change to make it work, any help? Commented Dec 21, 2011 at 22:19
  • @david: How is exactly is it not working? What is the expected output and how does that differ from the output that you get? Commented Dec 21, 2011 at 22:22
  • 1
    Did you try System.out.println("Number is a happy number"); in if(sum == 1) and it's not working or what? Commented Dec 21, 2011 at 22:29

1 Answer 1

2

I don't really understand your code. However this is how I would do:

public int[] intToDigitArray(int number)
{
    int numberOfDigits = (int) (Math.ceil(Math.log(number) / Math.log(10.0f)));
    int[] digits = new int[numberOfDigits];

    for (int i = 0; i < numberOfDigits; ++i)
    {
        int digit = (number % powerOfTen(i + 1)) / powerOfTen(i);
        digits[numberOfDigits - i - 1] = digit;
    }

    return digits;
}

public int powerOfTen(int exponent)
{
    if (exponent == 0) return 1;
    return 10 * powerOfTen(exponent - 1);
}

Tested, and works:

System.out.println(Arrays.toString(intToDigitArray(1234445)));

Prints:

[1, 2, 3, 4, 4, 4, 5]
Sign up to request clarification or add additional context in comments.

1 Comment

Are you sure this works, I copied it into a compiler and it did not work as expected. Have you made late minute changes? What about the input 0?

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.