java - Finding the middle letter and make it uppercase.. am stuck and the end .. :( -
here finding middle letter of each element of array , making upper case , merge it'll give me result e:g- {boy, india, apple}
following code trying till now.
public class findmiddle {      public static void main(string[] args) {          arraylist<string> al = new arraylist<string>();          al.add("boy india apple");          string str[] = al.toarray(new string[al.size()]);         string newar[];         string delimiter = " ";          newar = str[0].split(delimiter);          (int = 0; < newar.length; i++) {             char[] c = newar[i].tochararray();             (int j = 0; j < c.length; j++) {                 if (j == c.length / 2) {                     c[j] = character.touppercase(c[c.length / 2]);                     system.out.println(c[j]);                 }                 newar[i] += c[j];             }         }     } } it's giving me o d p.
i want them merged original elements boy india apple.
that's because print middle char (the println inside if statement)
 if (j == c.length / 2) {         c[j] = character.touppercase(c[c.length / 2]);         system.out.println(c[j]);  } i suggest using stringbuilder.setcharat
 (int = 0; < newar.length; i++) {             stringbuilder updatestring = new stringbuilder(newar[i]);             int middleindex = newar[i].length /2;             updatestring.setcharat(middleindex, character.touppercase(newar[i].charat(middleindex));             system.out.println(updatestring);   } 
Comments
Post a Comment