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"));
    }
}
itsaferbie
  • 81
  • 4
  • 9
  • I would experiment with String variables, then look closely at String class functions - http://docs.oracle.com/javase/6/docs/api/java/lang/String.html – Caffeinated Nov 14 '13 at 01: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? – Johannes H. Nov 14 '13 at 01:49
  • @JohannesH. - sorting the letters inside the strings – Caffeinated Nov 14 '13 at 01:50
  • 1
    @Adel: that'S what I thought, too. Then my answer is suitable at least. ^^ – Johannes H. Nov 14 '13 at 01:51
  • Have you talked to your instructor about how to do this and what they want you to get out of it? –  Nov 14 '13 at 02:03
  • ... `Some code that I have just typed up.` - that code won't even compile. What effort have you put into solving this? –  Nov 14 '13 at 02:05
  • From what I understand, he wants us to get how to use for loops. – itsaferbie Nov 14 '13 at 02:05
  • possible duplicate of [Sorting individual elements in a String Array by their alphabetical order](http://stackoverflow.com/questions/19070264/sorting-individual-elements-in-a-string-array-by-their-alphabetical-order) –  Nov 14 '13 at 02:06
  • I know the code won't compile, I am still working on it. – itsaferbie Nov 14 '13 at 02:06
  • Also we can't use arrays to solve how to get them in alphabetical order. – itsaferbie Nov 14 '13 at 02:08
  • You have to look up the usage of for-loops again. First part is usually used to initialize counter variables (which you'Re doing before the loop starts, which isn't wrong per-se but ugly), while the code that you put in there is definitely wrong and needs to go inside the loop body. – Johannes H. Nov 14 '13 at 02:09
  • OK you're moving forward, code looks ok at 1st blush. One small thing - you wouldn't call `alphabetize(iunaselfksdf)` but rather `alphabetize("iunaselfksdf")` since argument is a String – Caffeinated Nov 14 '13 at 03:19
  • I'm actually runnin through logic on paper myself, this HW is a decent exercise. once you figure it out you'll be happy, it's a good one – Caffeinated Nov 14 '13 at 03:20

2 Answers2

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
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • Ughs, that's even less ineffective than basic "find smallest remaining element and insert it at the end of the result" – Johannes H. Nov 14 '13 at 02:18
  • I have no knowledge of sorting, and we haven't learned array's yet either. Array's we should be learning very soon though. – itsaferbie Nov 14 '13 at 02:23
  • @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... – Adrian Shum Nov 14 '13 at 02:23
  • @itsaferbie then try to understand my second approach. It should be something you can understand now. – Adrian Shum Nov 14 '13 at 02:24
  • I am pretty new to the coding scene as well, only been doing Java as of a month now. – itsaferbie Nov 14 '13 at 02:25
  • @Adrian: You may have a point there, and I really like your answer (and upvoted it), so I really didnt want to criticize it. But that doesn'T make the sorting algorithm any more effective :D – Johannes H. Nov 14 '13 at 02:25
  • I'm still a little confused, I haven't made much progress on the code either. What's mainly getting me is the currentChar, do I have to make it so that I compare the ascii values of them. – itsaferbie Nov 14 '13 at 02:40
  • @itsaferbie That's where `compareTo()` comes into play ;) – Johannes H. Nov 14 '13 at 02:52
  • @itsaferbie - Welcome to teh Java club! Quite a powerful language. learning curve is a little steep if you're new to programming – Caffeinated Nov 14 '13 at 03:23
  • 1
    Your telling me, my head feels like its on the verge of exploding haha. – itsaferbie Nov 14 '13 at 03:28
  • 1
    @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. – Adrian Shum Nov 14 '13 at 03:47
  • @itsaferbie - I have to echo Adrian, using console output (i.e calling `System.out.print(variableImCuriousAbout)` is a programmer's best friend - I know that for me it's like swiss army knife – Caffeinated Nov 14 '13 at 04:25
  • @Adel ... until you learn how to use the debugger in the IDE and set conditional breakpoints, or breakpoint on variable change, or just breakpoints in general and variable watch, eclipse's [display](http://stackoverflow.com/questions/4499944/how-to-use-eclipses-display-view-for-debug) panel... –  Nov 14 '13 at 15:45
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 ;) )

ajp15243
  • 7,704
  • 1
  • 32
  • 38
Johannes H.
  • 5,875
  • 1
  • 20
  • 40