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.

Recommended Answers

All 7 Replies

You should search through the forums a bit more :) .

http://www.daniweb.com/forums/thread133150.html

I have looked that up before, but the thing is that it deals with arrays which mine doesn't. The codes are written differently, so I don't really understand the other one that well.

I have looked that up before, but the thing is that it deals with arrays which mine doesn't. The codes are written differently, so I don't really understand the other one that well.

Well, what exactly are you sorting? What's the input and what is the output supposed to be?

Is it splitting a sentence into words, then sorting the words? If so, "All Big Cats" is already sorted and "Cats All Big" would turn into "All Big Cats". What's the goal here?

Well, what exactly are you sorting? What's the input and what is the output supposed to be?

Is it splitting a sentence into words, then sorting the words? If so, "All Big Cats" is already sorted and "Cats All Big" would turn into "All Big Cats". What's the goal here?

VernonDozier is right. Your code doesn't make any sense. What exactly are you trying to accomplish and what are your requirements.

Also the String class has this method:
"compareTo"
It is used to compare 2 strings.
If it returns 0 means that the Strings are equal.
If it returns positive the first is "greater" than the second
If it returns negative the first is "lower" than the second.

So if sorting is your game, you can use this method to sort in the way you would sort numbers:

String a="abc";
String b = "def";

if (a.compareTo(b)>0) { // a > b
  System.out.println("a should go after b");
} else { // a <= b
  System.out.println("a should go before b");
}

I'm not sure about this, but I think using Collections.sort(yourArray) would also do the same thing javaAddict is suggesting. But it seems like your assignment is to write the sorting method yourself, so you'll need to specify what vernon mentioned.

I'm not sure about this, but I think using Collections.sort(yourArray) would also do the same thing javaAddict is suggesting. But it seems like your assignment is to write the sorting method yourself, so you'll need to specify what vernon mentioned.

Actually we don't know what his assignment is, that is why I asked for the specifications. His code though seems that tries to compare 2 Strings, which is why I recommended that method.

Just add the strings in a TreeSet. All the Strings will be automatically sorted alphabetically.
You may check
SNIP
for more details on it. Though the code here is for numbers, it applies the same for Strings.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.