I WANT HELP ON THE FOLLOWING SORTING CODE. PROBLEM IS THAT I WANT TO SORT THE CHARACTERS IN STRING. 1ST I CONVERTED STRING IN CHAR[] ARRAY THEN I TRY TO SORT CHAR[] ARRAY. AND THEN I CONVERTED THAT CHAR BACK INTO STRING ..... it gives an nullpointer exception
public class StringSort { public static String s1,s2; public static int k; public static char x,z; public static char[] temp2;
public void sort() throws IOException { s1="tola"; k=s1.length(); char[] texttemp=s1.toCharArray();// t,o,l,a for (int p=0;p<k;p++) { x=texttemp[p]; z=texttemp[p+1]; if (x<z) { temp2[k]=x; //t,o temp2[k+1]=z; } else { temp2[k]=z;//o,t temp2[k+1]=x; }
}
s2=String.copyValueOf(temp2);//converting char to string System.out.print(" sorted characters in string is "+s2);
Format that code properly and someone might bother to respond to your post. This topic of coding formatting has been beaten to death, personal I'm not even going to spend 5 seconds looking at your ill-presented code!!!