java - How do I convert a String to array Integer? -
i trying convert string
array integer
,i made code working in java ,but in android studio array converted don't take last value of array covered string
string y="abc"; string[] array = y.split(""); int[] x = new int[y.length()]; (int i=0;i<y.length();i++) { string letter=array[i]; if( letter.equals("a")){x[i]=1;} if( letter.equals("b")){x[i]=2;} if( letter.equals("c")){x[i]=3;} sum =sum + x[i]; }
sum variable dont count last value of conversation ,it should 6 3
you should define "sum" outside loop. keep value.
string y="abc"; string[] array = y.split(""); int[] x = new int[y.length()]; int sum = 0; (int i=0;i<y.length();i++) { string letter=array[i]; if( letter.equals("a")){x[i]=1;} if( letter.equals("b")){x[i]=2;} if( letter.equals("c")){x[i]=3;} sum += x[i]; }
Comments
Post a Comment