String arr = "[1,2]";
String[] items = arr.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\\s", "").split(",");

int[] results = new int[items.length];

for (int i = 0; i < items.length; i++) {
    try {
        results[i] = Integer.parseInt(items[i]);
    } catch (NumberFormatException nfe) {
        //NOTE: write something here if you need to recover from formatting errors
    };
}
Answer from Saul on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί java β€Ί java-program-to-convert-string-to-integer-array
Java Program to Convert String to Integer Array - GeeksforGeeks
The above example uses the Stream API to split the string by parsing each substring into an integer, and collect the results into an integer array.
Published: July 23, 2025
Discussions

How to convert a String into an Integer[] array in Java? - Stack Overflow
If the String is something like "19 35 91 12 36 48 59" and I want an array of the same structure. ... There is no single method in Java API for that. More on stackoverflow.com
🌐 stackoverflow.com
Converting a String array into an int Array in java - Stack Overflow
I am new to java programming. My question is this I have a String array but when I am trying to convert it to an int array I keep getting java.lang.NumberFormatException My code is private void More on stackoverflow.com
🌐 stackoverflow.com
October 2, 2014
java - Is String to int Array conversion possible? - Stack Overflow
I've written that piece of code in which I scanned an integer suppose 121 and for dividing it into 3 part I make it a String and tried to convert it again by splitting.But I am not getting the way?... More on stackoverflow.com
🌐 stackoverflow.com
Java- Converting a string of numbers separated by spaces into an array?
You have the right idea. String.split() will return an array of strings. Then, if you want to treat any individual string in that array as an integer you can use Integer.parseInt() . If you want to create an array of integers, just make the array that way. More on reddit.com
🌐 r/learnprogramming
7
4
February 2, 2016
Top answer
1 of 6
79

You could read the entire input line from scanner, then split the line by , then you have a String[], parse each number into int[] with index one to one matching...(assuming valid input and no NumberFormatExceptions) like

String line = scanner.nextLine();
String[] numberStrs = line.split(",");
int[] numbers = new int[numberStrs.length];
for(int i = 0;i < numberStrs.length;i++)
{
   // Note that this is assuming valid input
   // If you want to check then add a try/catch 
   // and another index for the numbers if to continue adding the others (see below)
   numbers[i] = Integer.parseInt(numberStrs[i]);
}

As YoYo's answer suggests, the above can be achieved more concisely in Java 8:

int[] numbers = Arrays.stream(line.split(",")).mapToInt(Integer::parseInt).toArray();  

To handle invalid input

You will need to consider what you want need to do in this case, do you want to know that there was bad input at that element or just skip it.

If you don't need to know about invalid input but just want to continue parsing the array you could do the following:

int index = 0;
for(int i = 0;i < numberStrs.length;i++)
{
    try
    {
        numbers[index] = Integer.parseInt(numberStrs[i]);
        index++;
    }
    catch (NumberFormatException nfe)
    {
        //Do nothing or you could print error if you want
    }
}
// Now there will be a number of 'invalid' elements 
// at the end which will need to be trimmed
numbers = Arrays.copyOf(numbers, index);

The reason we should trim the resulting array is that the invalid elements at the end of the int[] will be represented by a 0, these need to be removed in order to differentiate between a valid input value of 0.

Results in

Input: "2,5,6,bad,10"  
Output: [2,3,6,10]

If you need to know about invalid input later you could do the following:

Integer[] numbers = new Integer[numberStrs.length];
for(int i = 0;i < numberStrs.length;i++)        
{
    try 
    {
        numbers[i] = Integer.parseInt(numberStrs[i]);
    }
    catch (NumberFormatException nfe)   
    {
        numbers[i] = null;
    }
}

In this case bad input (not a valid integer) the element will be null.

Results in

Input: "2,5,6,bad,10"  
Output: [2,3,6,null,10]

You could potentially improve performance by not catching the exception (see this question for more on this) and use a different method to check for valid integers.

2 of 6
44

Line by line

int [] v = Stream.of(line.split(",\\s+"))
  .mapToInt(Integer::parseInt)
  .toArray();

With the Arrays.stream() alternative as

int [] v = Arrays.stream(line.split(",\\s+"))
  .mapToInt(Integer::parseInt)
  .toArray();

However much better for parsing bigger chunks of text is

int [] v = Pattern.compile(",\\s+").splitAsStream(line)
  .mapToInt(Integer::parseInt)
  .toArray();  

As this does not require the string array to be in memory, and we do an incremental parse to produce the integers. Moreover, as the input to splitAsStream is a CharSequece, and not just String, we can now also used buffered character sources to avoid even having the full input source in memory.

See also How do I create a Stream of regex matches? for some more interesting reading on incremental parsing.

🌐
Baeldung
baeldung.com β€Ί home β€Ί java β€Ί java array β€Ί converting a string array into an int array in java
Converting a String Array Into an int Array in Java | Baeldung
January 8, 2024 - In this tutorial, we will use Java’s minimum integer as the fallback for invalid string elements: int[] expectedWithInvalidInput = new int[] { 1, 2, Integer.MIN_VALUE, 4, Integer.MIN_VALUE, 6, 42 }; Next, let’s start with the string array with all valid elements and then extend the solution with the error-handling logic. For simplicity, we’ll use unit test assertions to verify if our solutions work as expected.
🌐
How to do in Java
howtodoinjava.com β€Ί home β€Ί java array β€Ί convert a string array to integer array in java
Convert a String Array to Integer Array in Java
January 20, 2023 - The array type declaration has been given in toArray(Integer[]::new) method. String[] strArray = new String[] {"1", "2", "3"}; Integer[] integerArray = Arrays.stream(strArray) .map(Integer::parseInt) .toArray(Integer[]::new); System.out.pri...
🌐
Delft Stack
delftstack.com β€Ί home β€Ί howto β€Ί java β€Ί convert string to int array
How to Convert String to Int Array in Java | Delft Stack
February 2, 2024 - Iterate to convert the String tokens to int and collect in an int array. package stringToIntArray; import java.util.StringTokenizer; public class StringToIntUsingStringTokenizer { public static void main(String[] args) { String testString = "[1,2,3,4]"; StringTokenizer stk = new StringTokenizer(testString, "[,]"); String[] strings = new String[stk.countTokens()]; int[] integerArray = new int[stk.countTokens()]; int i = 0; while (stk.hasMoreTokens()) { strings[i] = stk.nextToken(); integerArray[i] = Integer.parseInt(strings[i]); i++; } for (int j = 0; j < integerArray.length; j++) System.out.println("number[" + j + "]=" + integerArray[j]); } }
Find elsewhere
🌐
Quora
quora.com β€Ί How-do-I-convert-a-string-to-integer-array-in-Java
How to convert a string to integer array in Java - Quora
Answer (1 of 24): There are two ways of converting a string to integer in java. 1- Integer.parseInt() Example to convert a String β€œ50” to a primitive int. > [code]String number = "50"; int result = Integer.parseInt(number); System.out.println(result); [/code] OutPut > [code]50 [/code] If...
🌐
TutorialsPoint
tutorialspoint.com β€Ί article β€Ί How-to-convert-string-to-array-of-integers-in-java
How to convert string to array of integers in java?
September 6, 2023 - Java Object Oriented Programming ... class. To convert a string array to an integer array, convert each element of it to integer and populate the integer array with them....
🌐
Java Code Geeks
javacodegeeks.com β€Ί home β€Ί core java
How to Split a String into an Int Array in Java - Java Code Geeks
November 10, 2025 - 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.
🌐
Coderanch
coderanch.com β€Ί t β€Ί 728526 β€Ί java β€Ί Converting-String-Int-Array
Converting String to Int Array (Beginning Java forum at Coderanch)
March 29, 2020 - We usually avoid giving out complete code, but it can't do any harm now you have handed your assignment in:-You can only use var to represent a type in Java10+ and only for local variables; you could use int/String/Scanner instead. It is awkward (if not impossible) to use var for an array.
🌐
Sanfoundry
sanfoundry.com β€Ί java-program-convert-string-integer-array
Java Program to Convert a String to an Integer Array - Sanfoundry
May 23, 2022 - This is the Java Program to Convert a String to an Integer Array. ... Given a string consisting of various numbers separated by spaces, convert it into an array of Integers, such that every number occupies a position in the array, and for every invalid number, there is a -1 in the array.
🌐
CodeSpeedy
codespeedy.com β€Ί home β€Ί how to convert string array to int array in java with an example
convert string array to int array in java with an example - CodeSpeedy
November 1, 2018 - In order to convert a string array to an integer array, I will first convert each element of the string array to integer. Then I will populate the Integer array with those elements.
🌐
YouTube
youtube.com β€Ί njsoftware
Turn a String Array into an Integer Array in Java - YouTube
This video is about turning a String Array into an Integer Array in Java
Published: March 8, 2017
Views: 14K
🌐
Sololearn
sololearn.com β€Ί en β€Ί Discuss β€Ί 1962021 β€Ί how-do-you-convert-a-string-array-list-to-an-integer-array-list-in-java
How do you convert a String array list to an integer ...
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
YouTube
youtube.com β€Ί watch
String Array To String || Integer Array To Integer || Mini Java Interview Questions Series - YouTube
String Array To String || Integer Array To Integer || Mini Java Interview Questions SeriesSchedule a meeting in case of any queries/guidance/counselling:http...
Published: July 29, 2022
🌐
Reddit
reddit.com β€Ί r/learnprogramming β€Ί java- converting a string of numbers separated by spaces into an array?
r/learnprogramming on Reddit: Java- Converting a string of numbers separated by spaces into an array?
February 2, 2016 -

For example, I have String input which will look something like "5 6 6" but I need it to be converted into an array. What is the easiest way to do this? I have messed around with .split and .parseInt but have not come to any solid conclusions. Thanks!

🌐
Coderanch
coderanch.com β€Ί t β€Ί 381786 β€Ί java β€Ί Pls-convert-string-array-int
Pls HELP-- convert string array to int array (Java in General forum at Coderanch)
January 4, 2007 - You can convert this as: code: String [] chckbox = request.getParameterValues("jspchckbox"); int[] value = new int[chckbox.length]; for(int i=0; i<chckbox.length; i++) value[i] = Integer.parseInt(chckbox[i]); This is what I did, but it is only pick the first item, i check 4 checkboxes on the ...