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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -