HOWTO · Java
How to Get the Separate Digits of an Int Number in Java
Learn four Java approaches to extract and print the digits of an integer, with examples and input limitations.
On this page
To print the digits of a positive int in their original order, you can extract them with % and /, or convert the number to a string and inspect its characters. This guide shows those approaches and a recursive alternative, using 12223 throughout.
% (mod) to Get the Remainder of the Given Integer Number
For a positive number, % 10 gives its rightmost digit and / 10 removes that digit. Extraction therefore runs from right to left. Store each remainder in a LinkedList stack, then remove the values to print them in the original order. This example is for positive int values; zero and negative inputs need separate handling.
The loop stops as soon as the remaining value becomes zero, which is why zero would leave the list empty and print no digits. If zero is valid input, add a special case that prints 0. Java’s remainder for a negative value can itself be negative, so this loop is not a general signed-number solution. You could first record the sign and process an absolute magnitude, taking care with Integer.MIN_VALUE, whose positive magnitude cannot be represented as an int. For an int that may include that value, use a long magnitude or choose a string-based approach instead. This arithmetic method is useful when you need numeric digit values directly, for example to sum them.
Run this example:
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
int number = 12223;
LinkedList<Integer> digits = new LinkedList<>();
while (number > 0) {
digits.push(number % 10);
number /= 10;
}
while (digits.size() > 0) {
System.out.println(digits.pop());
}
}
}
1
2
2
2
3
String.toCharArray() to Get the Array of Characters
Convert the integer to a string, then call toCharArray() to get one character for each digit. A char such as '2' is a character, not the numeric value 2; use Character.digit when the calculation needs an integer digit. This version is scoped to positive input, so the resulting array contains only digits.
This approach keeps the input’s displayed digit order and is easy to adapt for validation or formatting. For numeric work, Character.digit(digit, 10) maps each decimal digit character to its integer value; check for a negative return value if the source can contain non-digit characters. Converting a negative integer to a string includes '-' as the first character, so skip or handle that sign separately instead of treating it as a digit. Zero converts to the one-character string "0", so this approach naturally retains its digit. Since int has a bounded range, this converts the complete decimal representation of that value, not digits from a larger numeric type or from an input string with leading zeros.
public class Main {
public static void main(String[] args) {
int number = 12223;
char[] digits = String.valueOf(number).toCharArray();
for (char digit : digits) {
System.out.println(Character.digit(digit, 10));
}
}
}
1
2
2
2
3
number.split("(?<=.)") Method to Get the String Array and Then Split Them
String.split returns an array of strings. The regular expression (?<=.) matches a position immediately after a character, so the positive integer string is split into one string per digit. For a number with a minus sign, the sign is also an element; toCharArray() is usually simpler when you want characters rather than strings.
Each array element remains text, so parse an element only when later calculations require an integer value. The string conversion also means a value of zero yields "0", while a negative value yields a leading sign element followed by its digits; filter that sign before parsing digits. This expression is handy when the rest of a task already expects strings, but it adds regular-expression and array overhead without improving on toCharArray() for a simple character traversal. As with the other integer-to-string approach, it sees the numeric value’s representation: it cannot recover leading zeros that were absent from the original int.
public class Main {
public static void main(String[] args) {
int number1 = 12223;
String number = String.valueOf(number1);
String[] digits = number.split("(?<=.)");
for (String digit : digits) {
System.out.println(digit);
}
}
}
1
2
2
2
3
Separate Digits From an Int Using Recursion
Recursively divide a positive number by 10 until it reaches zero. Print each remainder after the recursive call returns; that order prints the leftmost digit first. This version prints nothing for zero and does not process negative inputs, so add explicit handling if those values are valid for your application.
The stopping condition is the base case: once division reaches zero, calls return without printing, and the pending calls print their saved remainders while unwinding. That call stack reverses the extraction order without a separate collection. For input zero, add a direct print because the method’s condition is false on the first call. For negative input, handle and print the sign before recursing on a safe magnitude; also account for Integer.MIN_VALUE if every possible int is allowed. Recursion is clearest as a teaching example for a short integer, while an iterative loop is usually easier to extend and does not use one stack frame per digit.
public class Main {
public static void main(String[] args) {
recursion(12223);
}
public static void recursion(int number) {
if (number > 0) {
recursion(number / 10);
System.out.println(number % 10);
}
}
}
1
2
2
2
3