Java Program to Convert String to Integer Array
Last Updated :
19 Nov, 2024
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. In this article, we will discuss different methods for converting a numeric string to an integer array in Java.
Example:
Below is the most concise and ideal for processing space-separated numbers or similar patterns to convert string to integer array.
Java
import java.util.Arrays;
public class StrToIntArr {
public static void main(String[] args) {
String s = "1 2 3 4 5";
// Using 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("" + Arrays.toString(arr));
}
}
Output1 2 3 4 5
[1, 2, 3, 4, 5]
Explanation:
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.
Other Methods to Convert String to Integer Array in Java
There are 2 methods to convert a String to an Integer Array, which are as follows:
1. Using replaceAll() and split() methods (for Strings with Brackets or Other Delimiters like Commas)
The string.replaceAll() method takes two arguments, a regex, and the replacement values. This method will replace the given regex with the given replacement value, and after that, the split() method is used to split the string.
Example:
Java
// Java Program to Convert String to Integer Array using string.replaceAll() method
import java.io.*;
import java.util.Arrays;
public class GFG {
public static void main(String[] args) {
String s = "[1,2,3,4,5]";
// calling replaceAll() method and split() method on string
String[] s1 = s.replaceAll("\\[", "")
.replaceAll("]", "")
.split(",");
int[] arr = new int[s1.length];
// parsing the String argument as a signed decimal
// integer object and storing that integer into the
// array
for (int i = 0; i < s1.length; i++) {
arr[i] = Integer.valueOf(s1[i]);
}
System.out.print("String: " + s);
System.out.print("\nInteger array: "
+ Arrays.toString(arr));
}
}
OutputString: [1,2,3,4,5]
Integer array: [1, 2, 3, 4, 5]
2. Using split() and Integer.parseInt() method (for Space-Separated Numbers)
The string.split() method is used to split the string into various sub-strings. Then, those sub-strings are converted to an integer using the Integer.parseInt() method and store that value integer value to the Integer array.
Example:
Java
// Java Program to Convert String to Integer Array using string.split() method
import java.io.*;
public class GFG {
static int[] method(String s) {
String[] sa = s.split(" ");
int[] arr = new int[sa.length];
// parsing the String argument as a signed decimal
// integer object and storing that integer into the array
for (int i = 0; i < sa.length; i++) {
arr[i] = Integer.parseInt(sa[i]);
}
return arr;
}
public static void main(String[] args)
{
// Bufferedreader declaration
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
String s = "1 2 3 4 5";
int i;
// declaring the variable & calling the method with passing string as an argument
int[] arr = method(s);
System.out.print("\nString : " + s);
System.out.print("\nInteger array : [");
for (i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.print("]");
}
}
OutputString : 1 2 3 4 5
Integer array : [1 2 3 4 5 ]