+ 1
How to covert list of Strings to int ?
example String 12,34,57,13 to int 12 34 57 13 use split
12 Réponses
+ 1
Shamsulhaq alkozay s is the name of the variable that stores an array of strings.
It has 4 values currently so the length is 4.
The values in the containers currently are:
s[0] = "12", s[1] = "34", s[2] = "57", s[3] = "13"
Since we did String[], the values are stored as strings, and mathematical calculations cannot be applied to them.
int[] i = new int[s.length] creates a new array variable.
We call it i. Since we do not have values for it yet, we set the size to the length of the String array. (in this case it is 4, but if you add more values to the String array, the int array size will increase accordingly.
The for loop basically takes each value in the s array, converts it to an integer, and stores it in the respective i array container.
+ 4
Than you need an array of integers and do accordingly.
String[] s = {"12", "34", "57", "13"};
int[] i = new int[s.length];
for(int x = 0; x < i.length; x++) {
i[x] = Integer.parseInt(s[x]);
}
+ 3
Split? If you mean a long string without an array, such as
longString = "12, 34, 56, 13";
You can simply do
String[] s = longString.split(",");
Then the remaining integer codes can remain.
+ 2
Shamsulhaq alkozay
So with the above example, you see:
i[0] = Integer.parseInt(s[0]);
i[1] = Integer.parseInt(s[1]);
i[2] = Integer.parseInt(s[2]);
i[3] = Integer.parseInt(s[3]);
which becomes:
i[0] = Integer.parseInt("12");
i[1] = Integer.parseInt("34");
i[2] = Integer.parseInt("56");
i[3] = Integer.parseInt("13");
resulting in:
i[0] = 12, i[1] = 34, i[2] = 56, i[3] = 13
+ 2
I have never used cmd with Java so I have no idea how to do that, sorry.
But if you are using cmd and know how to use Java, just get the variable name and split it. I can't help you much further without this unfortunately.
+ 1
thanks again
0
String s = "21"
int i = Integer.parseInt(s)
0
list of String
it means that there we use array
0
could you please explain your answer how it works.
0
How to split them
0
I mean if we get values through cmd.
how can we split the code to int for mathematical calculations in the same example which you explained.
0
where can we put that split code