Convert a String array to an int array in Java
This post will discuss how to convert a string array to an int array in Java.
1. Using Stream API
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 elements into an int primitive array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; public class Main { public static void main(String[] args) { String[] strings = { "1", "2", "3", "4", "5"}; int[] values = Arrays.stream(strings) .mapToInt(Integer::parseInt) .toArray(); System.out.println(Arrays.toString(values)); } } |
Output:
[1, 2, 3, 4, 5]
Note that the Integer.parseInt() method throws NumberFormatException if the string is not parsable. You can also obtain a stream consisting of elements from the String array using the static factory method Stream.of().
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.stream.Stream; public class Main { public static void main(String[] args) { String[] strings = { "1", "2", "3", "4", "5"}; int[] values = Stream.of(strings) .mapToInt(Integer::parseInt) .toArray(); System.out.println(Arrays.toString(values)); } } |
Output:
[1, 2, 3, 4, 5]
If you need an Integer array instead of a primitive int array, you can do like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; public class Main { public static void main(String[] args) { String[] strings = { "1", "2", "3", "4", "5"}; Integer[] values = Arrays.stream(strings) .map(Integer::parseInt) .toArray(Integer[]::new); System.out.println(Arrays.toString(values)); } } |
Output:
[1, 2, 3, 4, 5]
Finally, if you need a List<Integer> from a String[], you can use collectors to collect the stream elements into a List.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { String[] strings = { "1", "2", "3", "4", "5"}; List<Integer> values = Arrays.stream(strings) .map(Integer::parseInt) .collect(Collectors.toList()); System.out.println(values); } } |
Output:
[1, 2, 3, 4, 5]
2. Using Custom Routine
Another solution is to write your own custom method for this easy task, which creates a new array and copy the elements from the original array to the new array after converting each element from string to the integer. A typical implementation for this approach would look like below.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; public class Main { public static void main(String[] args) { String[] strings = {"1", "2", "3", "4", "5"}; int[] values = new int[strings.length]; for (int i = 0; i < strings.length; i++) { values[i] = Integer.parseInt(strings[i]); } System.out.println(Arrays.toString(values)); } } |
Output:
[1, 2, 3, 4, 5]
Note that all the above solutions throw a NumberFormatException if any of the strings does not contain a parsable integer. That’s all about converting a string array to an int array in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)