1

I am trying to separate digits from an Integer and then put them into an array.

All elements, except for the first, are printing as 0. Could someone explain why this is it happening?

public class Doom{
    public static void main(String[] args){
        int number = 1234;
        int[] list = new int[5];
        while (number > 0) {

            int x = 0;
            int fork = (number%10);
            System.out.println(fork);
            list[x] = fork;
            x++;
            number = number / 10;
        }
        for (int x : list){
            System.out.println(x);
        }

    }
}
3
  • 2
    Your problem, aside from the best variable names, is that int x = 0; is inside your while loop. It keeps getting reset to 0 on every iteration. Commented Jan 23, 2017 at 21:14
  • x = 0 is executed on every iteration… Commented Jan 23, 2017 at 21:14
  • 1
    lol thanks, i am feeling so stupid now. Commented Jan 23, 2017 at 21:16

4 Answers 4

2

The problem is that you are declaring x inside the loop, so it gets reset to 0 each time. You want to move the int x = 0; line to be above the while (number > 0) { line, outside of the loop. Then it will be initialized to 0 only once, and each pass through the loop can assign it a new value with the x++ line.

Sign up to request clarification or add additional context in comments.

Comments

1

You keep redeclaring x in your loop, causing only the first index to have meaningful data. Move it to the outside of your loop.

Comments

0

There are quite a few errors in your program. Biggest one is that you are inializing x to 0 and then incrementing it by 1 within the while loop. It will keep storing your digits at the same location (0th place). Compare the following snippet and try to learn your mistakes:

public class Doom{
    public static void main(String[] args){
        int number = 1234;
        int[] list = new int[String.valueOf(number).length()];
        int x = 0;
        while (number > 0) {        
            int fork = (number%10);
            System.out.println(fork);
            list[x] = fork;
            x++;
            number = number / 10;
        }
        for (int y : list){
            System.out.println(y);
        }

    }
}

4 Comments

It can be initialized to 0 too. But then you need to do list[x] = fork. Just updated my answer accordingly.
How can i use the length of the integer directly? i mean like something that would do int[] list = new int[number.length] , without errors ofcourse
@KillerHawx, you can get the length (number of digits) of 'number' by this way: int length = String.valueOf(number).length();
@KillerHawx, I just edited the answer to reflect that.
0

You are reinitializing x to 0 every time. Declare it outside of the while loop.

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.