0

how to convert a string into int array

for eg. INPUT: string st="123"; OUTPUT: int ar[]={1,2,3};

28th Dec 2019, 9:13 AM
Somya Sarathi Samal
Somya Sarathi Samal - avatar
7 Answers
+ 2
In Java you can use something like that : public class Program { public static void main(String[] args) { String st = "123"; int[] array = new int[3]; for (int i = 0; i < 3; i++ ){ array[i] = (st.charAt(i)); System.out.println(array[i]-48); } } } Use the length of the String and so on.
28th Dec 2019, 10:41 AM
Julio Codesal
Julio Codesal - avatar
28th Dec 2019, 9:20 AM
šŸ‡®šŸ‡³OmkaršŸ•‰
šŸ‡®šŸ‡³OmkaršŸ•‰ - avatar
+ 2
Ok you you are getting chars type from your String. The char value for 1 is 49, for two 50 and so on... This way you get the correct value. https://www.w3resource.com/java-exercises/basic/java-basic-exercise-41.php
28th Dec 2019, 11:44 AM
Julio Codesal
Julio Codesal - avatar
+ 1
Julio Codesal thanks ;)
28th Dec 2019, 11:46 AM
Somya Sarathi Samal
Somya Sarathi Samal - avatar
0
Julio Codesal what is the logic behind the last line System.out.println(array[I]-48);
28th Dec 2019, 11:14 AM
Somya Sarathi Samal
Somya Sarathi Samal - avatar