Working with numbers stored as text is a common task in Java applications. Data from files, user input, APIs, or configuration values often comes in string form and needs to be converted into numerical types. A regular need is converting a delimited string (e.g., "10,20,30") into an integer array (int[]). This article explores different approaches to splitting a string and converting the results into integers efficiently and safely in Java.
1. Using String.split() + Integer.parseInt()
The most common way to convert a delimited string into an integer array in Java is by combining the split() method with Integer.parseInt(). This approach allows you to break the string into smaller pieces based on a delimiter such as a comma or space, and then convert each piece into a numeric value.
public class StringToIntArray {
public static void main(String[] args) {
String input = "1,2,3,4";
String[] substrings = input.split(",");
int[] numbers = new int[substrings.length];
for (int i = 0; i < substrings.length; i++) {
numbers[i] = Integer.parseInt(substrings[i].trim());
}
System.out.print("Converted int array: ");
for (int number : numbers) {
System.out.print(number + " ");
}
}
}
In the example above, the string is first split into an array of substrings using a comma as the delimiter. Each substring is then trimmed to remove unnecessary whitespace and converted into an integer. Finally, all parsed integers are stored in an int[], making the result ready for numerical processing.
When you run the program, you should see the following output:
Converted int array: 1 2 3 4
This confirms that the string was successfully split using the comma delimiter, each element was parsed into an integer, and all values were stored correctly in the array.
2. Using Java Streams (Java 8+)
Java Streams provide a more concise and expressive way to convert a string into an int array. Instead of manually looping through each piece of data, streams allow us to split the string, transform each value, and collect the results into an array in a single readable chain of operations.
public class StreamSplitStringToIntArray {
public static void main(String[] args) {
String input = "10,20,30,40";
int[] numbers = Arrays.stream(input.split(","))
.map(String::trim)
.mapToInt(Integer::parseInt)
.toArray();
System.out.print("Converted int array using Streams: ");
for (int number : numbers) {
System.out.print(number + " ");
}
}
}
In this example, the split() method still breaks the string into smaller segments, but Streams take over from there. Each substring is trimmed and converted to an integer using mapToInt(Integer::parseInt), and the final values are collected into an int[] with toArray(). This method is more concise and removes the need for manual loops.
3. Handling Invalid Numbers Safely
When dealing with user input or external data sources, there’s always a chance that the string may contain characters or values that aren’t valid integers. Instead of letting the program throw a NumberFormatException, we can add a validation step to ensure that only proper numeric values are processed. Using Java Streams, we can filter out any elements that don’t match a valid integer pattern before converting them.
public class SafeStringToIntArray {
public static void main(String[] args) {
String input = "10, 20, x, 40, -5, data";
int[] numbers = Arrays.stream(input.split(","))
.map(String::trim)
.filter(s -> s.matches("-?\\d+")) // Keep only valid integers
.mapToInt(Integer::parseInt)
.toArray();
System.out.print("Valid converted int array: ");
for (int number : numbers) {
System.out.print(number + " ");
}
}
}
In this example, the filter() method ensures that only elements matching a valid integer pattern are included in the final array. Any non-numeric values, such as "x" or "data", are simply skipped. This approach adds a layer of safety and prevents unwanted crashes when working with unreliable or user-generated input.
When the program runs, only the valid integers from the string are included in the result. The console will display:
Valid converted int array: 10 20 40 -5
This confirms that invalid values were safely filtered out during conversion.
4. Splitting by Multiple Delimiters
Sometimes a string may contain more than one type of separator, such as commas, spaces, or semicolons. In such cases, using a regular expression in the split() method allows Java to recognise multiple delimiters at once.
public class MultiDelimiterSplitExample {
public static void main(String[] args) {
String input = "10, 20;30 40";
int[] numbers = Arrays.stream(input.split("[,; ]+"))
.mapToInt(Integer::parseInt)
.toArray();
System.out.print("Array from multiple delimiters: ");
for (int number : numbers) {
System.out.print(number + " ");
}
}
}
In this example, the regular expression [,; ]+ tells Java to split the string wherever a comma, semicolon, or space appears. Each substring is then converted into an integer, giving you a clean array of numbers even when the input isn’t uniformly formatted.
After running the program, the integer array is successfully created from the string containing mixed delimiters. The console output will be:
Array from multiple delimiters: 10 20 30 40
This shows that commas, semicolons, and spaces were all handled correctly during the split operation.
5. Conclusion
In this article, we explored several techniques to split a string into an int array in Java. From the traditional String.split() and Integer.parseInt() approach to more modern Java Streams and safer validation strategies, each method offers flexibility depending on the formatting and reliability of the input data. We also demonstrated how to handle multiple delimiters and filter out invalid values to ensure accurate conversions.
6. Download the Source Code
This was a quick guide on how to split a string into an int array in Java.
You can download the full source code of this example here: java split string into int array
Thank you!
We will contact you soon.
Omozegie AziegbeNovember 10th, 2025Last Updated: November 10th, 2025

This site uses Akismet to reduce spam. Learn how your comment data is processed.