If the String
is something like "19 35 91 12 36 48 59"
and I want an array of the same structure.
I already tried
array[]=Integer.parseInt(str);
I'd split the string, stream the array, parse each element separately and collect them to an array:
int[] result = Arrays.stream(str.split(" ")).mapToInt(Integer::parseInt).toArray();
if they are separated by spaces you can convert them one by one like this
String array = "19 35 91 12 36 48 59";
// separate them by space
String[] splited = array.split(" ");
// here we will save the numbers
int[] numbers = new int[splited.length];
for(int i = 0; i < splited.length; i++) {
numbers[i] = Integer.parseInt(splited[i]);
}
System.out.println(Arrays.toString(numbers));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With