This program is supposed to take the array of command line arguments and sort them alphabetically. Problem is I'm getting an ArrayOutOfBoundsException somewhere. I also have a problem with the while loop. The way I've decided to alphabetize the array with looks a little bit shoddy. My plan was to examine the array in pairs, and keep swapping args.length*2 times until it was done.
ie.
bcda <-original
bcad <-swapped back once
bacd <-twice
abcd <-done
However I dont feel like that will work for all cases. Does anyone have a better was of doing this? I am a beginner so I only have a "beginner toolkit" to work with.
public class AlphaB2 {
public static void main(String[] args) {
int count = 0;
while(count < (args.length * 2)){
for(int i=0;i<args.length;i++){
if(args[i].compareToIgnoreCase(args[i+1])<0){ //if first is after second
String temp1 = args[i];
String temp2 = args[i+1];
args[i] = temp1;
args[i+1] = temp2;
}
}//end for
count++;
}//end while
}//end main
}//end class