1

What I need to do is take a string that has random letters and take the letters and put them in alphabetical order. For example, lidfj, would be; dfijl.

What I am having trouble figuring out is how I should start the code. I know that I may need to use compare to, since I am not allowed to use arrays, but I'm not sure if that would be the best approach. And I'm not sure how I would start that either.

Thanks in advance.

Edit: I think I'm done for the night, since I can't seem to think of anything else.

public class PP426 {
    public static String alphabetize (String input) {
        String sorted = ";
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if () {
                sorted += character;
            }
        }

        return sorted;

    }
    public static void main(String[] args) {
        System.out.println(alphabetize("iunaselfksdf"));
    }
}
13
  • I would experiment with String variables, then look closely at String class functions - docs.oracle.com/javase/6/docs/api/java/lang/String.html Commented Nov 14, 2013 at 1:47
  • SOoooo... wait. Just read Masud's answer, and now I'm not sure: Is it avbout sorting the strings (that is, which string comes first), or is it about osrting the letters inside the strings? Commented Nov 14, 2013 at 1:49
  • @JohannesH. - sorting the letters inside the strings Commented Nov 14, 2013 at 1:50
  • 1
    @Adel: that'S what I thought, too. Then my answer is suitable at least. ^^ Commented Nov 14, 2013 at 1:51
  • Have you talked to your instructor about how to do this and what they want you to get out of it? Commented Nov 14, 2013 at 2:03

2 Answers 2

5

From your questions and comments, seems that it is a homework assignment, for which you are allowed to use only String class for sorting (it is obviously not efficient but... well... it is a homework anyway).

Here I assume you already have idea on how to do sorting (e.g. bubble sort) with array. If not, search for the simplest sorting algorithm like insertion sort and learn it.

If you know how to sort with an array, you can simply translate your logic to use String's methods.

For example:

  • accessing character at position: yourString.charAt(i)
  • setting character at position: yourString = yourString.substring(0, i) + charToSet + yourString.substring(i+1)
  • appending a character: yourString += charToSet

If you don't even have knowledge in sorting, here is one method that works which involves only String:

I am not going to give you actual code. Learn it by yourself:

for currentChar in 'a' to 'z'
  loop through each char in inputString
    if you encounter any char = currentChar then 
       append that character to resultString
    end if 
  end loop
end for

resultString contains the result you need
Sign up to request clarification or add additional context in comments.

13 Comments

Ughs, that's even less ineffective than basic "find smallest remaining element and insert it at the end of the result"
I have no knowledge of sorting, and we haven't learned array's yet either. Array's we should be learning very soon though.
@JohannesH. I know my way is a bit tricky (as I assumed the string will contains only 'a'-'z') :P It just seems to me that OP is really a newbie and seems insertion sort is already too much for him... so I give this "tricky" method (in fact a bucket sort) which I believe is easier for OP to get hold of the concept...
Your telling me, my head feels like its on the verge of exploding haha.
@itsaferbie I know it is hard for a programming newbie to learn. Try to break the problem into smaller pieces and add feature bit by bit. e.g. Step 1: write a loop, and print out a to z. Step 2: instead of printing out a to z, in each iteration of loop, see if that character is available in the input string. Print it out if it exists. Step 3: Instead of printing the char out, append it to another string instead. I believe as a developer you should have the ability to solve these little problems by yourself.
|
2

Use a for loop and String.charAt() to traverse the string. Use a StringBuilder (for efficiency, concatenating Strings works as well but is meh performance wise) to assemble the letters in the order. WIth this knowledge, now implement you preferred sorting algorithm (which, I guess, is part of the excersise, so I won't do it here ;) )

5 Comments

We haven't learned about string builders yet, so I'm not sure what to do with StringBuilder.
Then just ignore that part (and use normal strings instead) and ask your teacher about it. Or come back once you got the sorting and I'll show you how to use the StringBuilder.
@itsaferbie - Stringbuillder is sort-of bonus. STring works ok
Would it be okay for me to paste some of the code that I have typed up currently to see if I'm on the right track?
Sure, I guess, go ahead!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.