The code below. Why won't my program sort the words. I can't figure out whats wrong with my code.
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author shawn.lim.2010
*/
public class Ch4Ex3 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String text = "This is my new program";
String delimiters = "[ ]";
// Analyze the string
String[] tokens = text.split(delimiters, 0);
String[] sorted = new String[tokens.length];
for (String token : tokens){
System.out.println(token);
}
for (int i = 0; i < (tokens.length-1); ++i){
int result = (tokens[i]).compareTo(tokens[i+1]);
if (result > 0){
sorted[i]=tokens[i+1];
sorted[i+1]=tokens[i];
} else {
sorted[i] = tokens[i];
sorted[i+1] = tokens[i+1];
}
}
System.out.println("\nThe sorted list of words is now: \n");
for (String sort : sorted){
System.out.println(sort);
}
}
}