Hi there.
I received an assignment to sort Strings alphabetically which a user inputs.
So far I've been able to produce the first word that would be top in the sort, so if someones inputs ("All Big Cats"), the first would be All.
Anyways that is where I am now stuck. I'm having trouble getting the other words to come after. Apparantly I'm supposed to have another method that uses the first method in order the produce the final result, using the return statement in the first method. I need help with the finalresult method.
What I have for the first method is this:
public static String sortline (String strword){
String str = strword;
String firstword="zzzzzzzzzzzzzzzzzzzzzz";
int indexstart=0;
for (int i=0; i <str.length();i++){
//System.out.println(indexstart+"");
if (Character.isLetter(str.charAt(i))){
if(i!=0 && !Character.isLetter(str.charAt(i-1))){
indexstart = i;
}
}else{
if(i!=0 && Character.isLetter(str.charAt(i-1))){
if ((firstword.compareToIgnoreCase(str.substring(indexstart,i)))>0){
firstword = str.substring(indexstart,i);
// System.out.println(firstword);
}
}
}
}
if (Character.isLetter(str.charAt(str.length()-1))){
if(str.substring(indexstart,str.length()).compareToIgnoreCase(firstword)<0){
firstword = str.substring(indexstart,str.length());
}
}
System.out.println("First word is:" +firstword);
return firstword;
}
Appreciate any help given.