Q:

Extracting digits of int in Java

So if I have an inputted integer:
int num_1 = 128

How would I be able to parse through the number and obtain a 1, 2 and 8, and assign  them to different variables?
Thanks!

A:

the inefficient way to do this would be to convert the integer to a string and iterate on the string characters.
the more efficient way would be something like:
int n = 128;
while (n > 0) {
  int d = n / 10;
  int k = n - d * 10;
  n = d;
  System.out.println(k);
}

A:

The answer that Thilo wrote is good but incomplete, you start by doing:
char[] digitsAsChars = String.valueOf(num_1).toCharArray();

and then:    
int[] digits = new int[charNums.length];
for(int i=0; i<charNums.length; i++){
   digits[i] = charNums[i]-48;//convert char to int
}

now digits holds the digits of the number as an int array

A:

try
while (num_1> 0){
            int digit = num_1%10;
            num_1 = num_1/10;
            System.out.println(digit);
        }

