-2

I want to Partition the input number to use its ciphers.
For example, I have an input number : 1563
How can I separate 1, 5, 6 and 3 and use them as separate integers?

package todicemal;

public class um {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int k=1563;
        System.out.println(k);
    }

}

How can I use the parts of k which form 1563 and use every single cipher as an integer?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Amna97al
  • 1
  • 3

3 Answers3

0
Stack<Integer> list = new Stack<Integer>();    
while (k > 0) {
  list.push(k % 10);
  k /= 10;
}
azizj
  • 3,428
  • 2
  • 25
  • 33
0

As pointed out in the answer here by jjnguy, you can just do:

int number; // = and int
LinkedList<Integer> stack = new LinkedList<Integer>();
while (number > 0) {
    stack.push( number % 10 );
    number = number / 10;
}

while (!stack.isEmpty()) {
    print(stack.pop());
}
Community
  • 1
  • 1
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
0

You can also do the following

int value = 1563;
char[] charValue = String.valueOf(value).toCharArray();

for (char c: charValue) {
    int num = Character.getNumericValue(c);
    //Use num
}
cswace
  • 1
  • 1