I have a string that contains numbers and I would like to put this numbers in a int array.
The format is like this:
String s = "234, 1, 23, 345";
int[] values;
What i want:
values[0]=234
values[1]=1
values[2]=23
values[3]=345
I have a string that contains numbers and I would like to put this numbers in a int array.
The format is like this:
String s = "234, 1, 23, 345";
int[] values;
What i want:
values[0]=234
values[1]=1
values[2]=23
values[3]=345
 
    
    You can split the string by comma, iterate through tokens and add them into another array by converting each token into integer, e.g.:
public static void main(String[] args){
    String s = "234, 1, 23, 345";
    String[] tokens = s.split(",");
    int[] numbers = new int[tokens.length];
    for(int i=0 ; i<tokens.length ; i++){
        numbers[i] = Integer.parseInt(tokens[i].trim());
    }
}
 
    
    Using Java8 :
String s = "234, 1, 23, 345";
String array[] = s.split(", ");
Stream.of(array).mapToInt(Integer::parseInt).toArray();
 
    
    Try splitting the String
String s = "234, 1, 23, 345";
String array[] = s.split(", ");
int a[] = new int[array.length];
for(int i=0;i<a.length;i++)
    a[i] = Integer.parseInt(array[i]);
 
    
    Look into using a String utility
String[] strings= s.split(","); 
and then loop over strings and add them to the int array using something like this
for(int i = 0; i < strings.size(); i++){
    values [i] = Integer.parseInt(strings[i]);
}
 
    
    You should Split, for-loop, and parse to int
String s = "234, 1, 23, 345";
String[] splittedS = s.split(", ");
int[] values = new int[splittedS.length];
for (int i = 0; i < splittedS.length; i++) {
    values[i] = Integer.parseInt(splittedS[i]);
}
System.out.println(Arrays.toString(values));
and if you are able to use streams java8
String[] inputStringArray = s.split(", ");
Integer[] convertedREsult = Arrays.stream(inputStringArray).map(Integer::parseInt).toArray(Integer[]::new);
System.out.println(Arrays.toString(convertedREsult));
