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
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 article, we’ve learned two ways to convert a string array to an integer array through examples. Moreover, we’ve discussed handling the conversion when the string array contains invalid number formats. If our Java version is 8 or later, the Stream API would be the most straightforward solution to the problem.
🌐
How to do in Java
howtodoinjava.com › home › java array › convert string array to int array in java (safe parsing)
Convert String Array to int Array in Java (Safe Parsing)
3 weeks ago - Converting a String[] to an int[] in Java means parsing every element with Integer.parseInt(), which a stream does in one line with Arrays.stream(texts).mapToInt(Integer::parseInt).toArray(); for an Integer[], we use map(Integer::valueOf) and ...
🌐
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 can use the static factory method Arrays.stream() to get a Stream for the array, convert each element to an integer using the Integer.parseInt() method, and then call the toArray() method to accumulate the stream ...
🌐
Javathinking
javathinking.com › blog › convert-strin-array-to-int-array-java
Converting String Array to Int Array in Java | JavaThinking.com
July 11, 2025 - Converting a string array to an integer array in Java is a common task in many programming scenarios. By understanding the core concepts, using the appropriate methods, and being aware of common pitfalls, you can perform this conversion effectively and avoid potential errors. Whether you choose to use a traditional for loop or the more concise Java 8 Stream API, make sure to follow the best practices to ensure the reliability of your code.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-string-to-integer-array
Java Program to Convert String to Integer Array - GeeksforGeeks
... import java.util.Arrays; public ... Streams to convert the string into an integer array int[] arr = Arrays.stream(s.split(" ")) .mapToInt(Integer::parseInt) .toArray(); System.out.println("" + s); System.out.println("" + ...
Published: July 23, 2025
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › java › java convert string array into int array
How to Convert String Array Into Int Array in Java | Delft Stack
March 13, 2025 - Converting a string array to an integer array in Java can be accomplished through various methods, each with its own advantages. Whether you choose to use a traditional loop, the Stream API, or a utility method, understanding these approaches will enhance your coding skills and improve your data handling capabilities.
🌐
CodingTechRoom
codingtechroom.com › question › convert-string-array-to-int-array-java-8
How Can I Convert a String Array to an Integer Array in Java 8? - CodingTechRoom
In Java 8, you can effortlessly convert a String array to an Integer array by utilizing the `Stream` API along with the `map` function.
🌐
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.
🌐
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
🌐
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...
🌐
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 - 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: ... 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. Java Streams provide a more concise and expressive way to convert a string into an int array.
🌐
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: ... send: Optional 'thank-you' note: Send · In general String array can be converted to int array by using Integer.parseInt() method....