+ 1

How to covert list of Strings to int ?

example String 12,34,57,13 to int 12 34 57 13 use split

4th Jul 2018, 4:43 AM
Shamsulhaq Alkozay
Shamsulhaq Alkozay - avatar
12 ответов
+ 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.
6th Jul 2018, 12:49 PM
Andre Daniel
Andre Daniel - avatar
+ 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]); }
4th Jul 2018, 5:08 AM
Andre Daniel
Andre Daniel - avatar
+ 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.
8th Jul 2018, 8:01 PM
Andre Daniel
Andre Daniel - avatar
+ 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
6th Jul 2018, 12:54 PM
Andre Daniel
Andre Daniel - avatar
+ 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.
9th Jul 2018, 2:43 AM
Andre Daniel
Andre Daniel - avatar
+ 1
thanks again
9th Jul 2018, 4:56 AM
Shamsulhaq Alkozay
Shamsulhaq Alkozay - avatar
0
String s = "21" int i = Integer.parseInt(s)
4th Jul 2018, 4:55 AM
Andre Daniel
Andre Daniel - avatar
0
list of String it means that there we use array
4th Jul 2018, 4:59 AM
Shamsulhaq Alkozay
Shamsulhaq Alkozay - avatar
0
could you please explain your answer how it works.
6th Jul 2018, 10:27 AM
Shamsulhaq Alkozay
Shamsulhaq Alkozay - avatar
0
How to split them
8th Jul 2018, 4:36 PM
Shamsulhaq Alkozay
Shamsulhaq Alkozay - avatar
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.
9th Jul 2018, 1:47 AM
Shamsulhaq Alkozay
Shamsulhaq Alkozay - avatar
0
where can we put that split code
9th Jul 2018, 1:49 AM
Shamsulhaq Alkozay
Shamsulhaq Alkozay - avatar