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.

Answer from Java Devil on Stack Overflow
🌐
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 - If our Java version is 8 or later, the Stream API would be the most straightforward solution to the problem. Otherwise, we can loop through the string array and convert each string element to an integer.
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.

Discussions

How to convert an array of string integers to an array of number integers
Essentially I have a select dropdown that is being populated by an API. If you look at the following code snippet, I essentially created an array of IDs called "first" and made it the value for the first select option above the map that I've done. Now in my handleChange when I log the value of a selected option it returns the value of the given option in an array of string numbers. --> Example... if user selects the second option ['1'] When the user selects 'All IDs' that's where the issue is. When that option is selected whats logged is ---> ['1,2,6,8,15,16,17,20,22,23,24,25,26,27,30,32,33,34,36'] I understand that I could use the split method but it crashes for any other option that's selected. How could i get around that? const DropDown = ({ list = [], title, onChange }) => { const handleChange = (e) => { const { value } = e.target const arr = [value] console.log(arr) // onChange(Number(value)) } const first = list.map((list) => list.id) return ( <> {title} {list.map((item) => ( {item.name} ))} ) } More on reddit.com
🌐 r/learnjavascript
7
4
May 3, 2022
Python: How do I convert an array of strings to an array of numbers? - Stack Overflow
MATHY people call Python lists an array. A 2-d list/array is a matrix. I believe the origin is from linear algebra. ... Save this answer. ... Show activity on this post. Use int which converts a string to an int, inside a list comprehension, like this: More on stackoverflow.com
🌐 stackoverflow.com
Converting a String array into an int Array in java - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... 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 More on stackoverflow.com
🌐 stackoverflow.com
October 2, 2014
Converting an array of string to array of int in PHP - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. ... Closed 2 years ago. ... Im guessing its because your variables are strings, and need to be integers. You might want to look into variable types, its a simple thing to solve really. Also, no need to shout. ... Save this answer. ... Show activity on this post. Use array... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 - String[] strArray = new String[] {"1", "2", "3"}; Integer[] integerArray = Arrays.stream(strArray) .map(Integer::parseInt) .toArray(Integer[]::new); System.out.println(Arrays.toString(integerArray)); //Prints [1, 2, 3] When we receive the string ...
🌐
Reddit
reddit.com › r/learnjavascript › how to convert an array of string integers to an array of number integers
r/learnjavascript on Reddit: How to convert an array of string integers to an array of number integers
May 3, 2022 -

I know this might be a silly question to ask but how could one covert the following into an array of numbers...

['1,2,6,8,15,16,17,20,22,23,24,25,26,27,30,32,33,34,36']

🌐
Laracasts
laracasts.com › discuss › channels › laravel › convert-string-array-to-array-of-ints
convert string array to array of int(s)
It necessary when he really wants an array of integers, if he don´t use it it will only be an array of strings even that the value seems to be an integer. ... Good call @wafto . I just double checked and you're 100% right, see below: array_map('intval', json_decode('["382","266","18","60"]')); ... Thanks @aswinghosh , you should mark @wafto below answer as correct, since he got it first and most accurately. https://laracasts.com/discuss/channels/laravel/convert-string-array-to-array-of-ints?reply=525914
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-string-to-array-of-integers-in-java
How to convert string to array of integers in java?
September 6, 2023 - You can convert a String to integer using the parseInt() method of the Integer class. To convert a string array to an integer array, convert each element of it to integer and populate the integer array with them.
🌐
Baeldung
baeldung.com › home › kotlin › kotlin strings › convert string array to int array in kotlin
Convert String Array to Int Array in Kotlin | Baeldung on Kotlin
March 11, 2024 - In its simplest scenario, a straightforward approach is to iterate through each String array element and use the toInt() function. It converts each element to an Int instance.
🌐
Techie Delight
techiedelight.com › home › java › convert a string array to an int array in java
Convert a String array to an int array in Java | Techie Delight
July 7, 2026 - If you use Java 8 or above, you ... array, convert each element to an integer using the Integer.parseInt() method, and then call the toArray() method to accumulate the stream elements into an int primitive array....
🌐
Coderwall
coderwall.com › p › 4dwucw › convert-string-array-to-int-array-3-steps
Convert string array to int array - 3 steps! (Example)
February 25, 2016 - #array · #convert · #c# #asp.net · char[] delimiters = new char[] { ',' }; string[] stringArray = "1,2,3,4,5".Split( delimiters ); int[] intArray = Array.ConvertAll( stringArray, s => int.Parse( s ) ); #.net · #csharp · #asp.net mvc · #array · #convert ·
🌐
Java2Blog
java2blog.com › home › python › convert string array to int array in python
Convert String Array to Int Array in Python [3 ways] - Java2Blog
December 25, 2022 - The eval() function accepts a Python string and evaluates it as a Python expression. We can use it in place of the int() function in the previous method. ... Use the map() function to apply the int() function to all elements of the array. Use the list() function to convert the map type object ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-string-to-integer-array
Java Program to Convert String to Integer Array - GeeksforGeeks
In Java, we cannot directly perform numeric operations on a String representing numbers. To handle numeric values, we first need to convert the string into an integer array.
Published: July 23, 2025
🌐
Coderanch
coderanch.com › t › 663219 › java › Converting-string-array-int-array
Converting a string array into an int array (Beginning Java forum at Coderanch)
March 11, 2016 - The text file looks like this: ... java.lang.Integer.parseInt(Integer.java:542) ... In general String array can be converted to int array by using Integer.parseInt() method....
🌐
Julia Programming Language
discourse.julialang.org › general usage
How can i convert number string array to int array - General Usage - Julia Programming Language
December 21, 2019 - Need help to convert string number array to int array Like [“0”, “1”, “0”, “1”, “0”, “1”, “1”, “1”] To [0, 1, 0, 1, 0, 1, 1, 1] NB: Without use this code [parse(Int, x) for x in array] because this method is too s…
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-array-of-strings-to-array-of-numbers-in-javascript
How to convert array of strings to array of numbers in JavaScript ? - GeeksforGeeks
July 23, 2025 - Converting an array of strings to an array of numbers in JavaScript involves transforming each string element into a numerical value.