Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:


1. Overview

When we work with integer numbers in Java, sometimes we need to break them into individual digits for various calculations or data manipulation tasks.

In this tutorial, we’ll explore various approaches to splitting an integer number into its constituent digits using Java.

2. Introduction to the Problem

As usual, let’s understand the problem through an example. Let’s say we have an integer number:

int THE_NUMBER = 1230456;

Our goal is to break the number into digits in order:

1, 2, 3, 0, 4, 5, 6

For simplicity, this tutorial will primarily concentrate on positive decimal integers. However, depending on specific needs, we might desire to obtain each digit as an Integer, a char, or a String.

Next, let’s explore various approaches to accommodate these different return types.

3. Splitting to an Integer List

First, let’s break the given integer into a list of Integers. That’s to say, we aim to get this list:

List<Integer> EXPECTED_INT_LIST = Lists.newArrayList(1, 2, 3, 0, 4, 5, 6);

One of the most straightforward approaches is to use a loop to repeatedly divide the number by 10, extracting the reminder in each iteration until the number becomes zero:

      1230456 / 10 = 123045 Reminder: 6
  |-> 123045 / 10 = 12304 Reminder: 5
  |-> 12304 / 10 = 1230 Reminder: 4
  |-> 1230 / 10 = 123 Reminder: 0
  |-> 123 / 10 = 12 Reminder: 3
  |-> 12 / 10 = 1 Reminder: 2
  |-> 1 / 10 = 0 Reminder: 1

However, as the example above shows, this approach generates the digits in the reverse order. To solve this, we can use LinkedList as a stack and push() the reminder to the stack in each step:

int number = THE_NUMBER;
LinkedList<Integer> result = new LinkedList<>();
while (number > 0) {
    result.push(number % 10);
    number /= 10;
assertEquals(EXPECTED_INT_LIST, result);

Also, we can use recursion to implement the same idea:

void collectDigits(int num, List<Integer> digitList) {
    if (num / 10 > 0) {
        collectDigits(num / 10, digitList);
    digitList.add(num % 10);

Then, we can call the collectDigits() method to get the result:

List<Integer> result = new ArrayList<>();
collectDigits(THE_NUMBER, result);
assertEquals(EXPECTED_INT_LIST, result);

If our Java version is Java 9 or later, we can use String.chars() and IntStream to get the digit list:

String numStr = String.valueOf(THE_NUMBER);
List<Integer> result = numStr.chars().map(Character::getNumericValue).boxed().collect(Collectors.toList());
assertEquals(EXPECTED_INT_LIST, result);

As the code above shows, we first convert the integer to a string. Then, the numStr.chars() method breaks the number string into chars as an IntStreamNext, Character‘s getNumericValue() gets the digits as ints from the char in the IntStream. Finally, we box ints into Integers and collect the result in a list.

4. Splitting to a char Array

Sometimes, we’d like to split the given integer into a char[] array:

char[] EXPECTED_CHAR_ARRAY = new char[] { '1', '2', '3', '0', '4', '5', '6' };

This task is straightforward, as the standard String class offers toCharArray() to break a string into a char[]What we need to do is simply convert the integer to String and call the toCharArray() method:

String numStr = String.valueOf(THE_NUMBER);
char[] result = numStr.toCharArray();
assertArrayEquals(EXPECTED_CHAR_ARRAY, result);

Of course, if required, we can convert the array to a list quickly.

5. Splitting to a String Array or String List

Finally, let’s see how to split an integer into a String[] array or String list:

String[] EXPECTED_STR_ARRAY = new String[] { "1", "2", "3", "0", "4", "5", "6" };
List<String> EXPECTED_STR_LIST = Lists.newArrayList("1", "2", "3", "0", "4", "5", "6");

Since we call this operation a “split,” the powerful split() method can help us solve the problem:

String numStr = String.valueOf(THE_NUMBER);
String[] result = numStr.split("(?<=.)");
assertArrayEquals(EXPECTED_STR_ARRAY, result);

As we can see, the code looks pretty compact. The key is the regex pattern we used in the split() function. (?<=.) is a positive lookbehind expression. For example, “(?<=X)a” matches every ‘a‘ that is after a ‘X‘ character.

However, we only have (?<=.) in our pattern. This makes the pattern match zero-width. So, split() takes the zero-width “character” after any character as the splitter. It might be easier to understand if we use the ‘#’ character to replace the zero-width “character” in the number string: “1#2#3#0#4#5#6#”. 

Further, split() discards the trailing empty elements by default. Therefore, it produces the desired result.

6. Conclusion

In this article, we’ve explored multiple methods to break an integer into digits. Furthermore, we’ve seen how to obtain the digits in different types, such as Integer, char, and String, through examples.

As always, the complete source code for the examples is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.