0

I have an exercise in class where you take names from the user and output them alphabetically. I'm not allowed to use Array.sort() or compareTo(). I seem to have gotten it to work for the most part... Except for when I enter something like a aaa aa, it outputs it in the following order:

aaa
aa
a

I'd really like to be able to output it in this order instead:

a
aa
aaa

This is what I have so far:

public static void main(String args[]) throws IOException {

    BufferedReader key =
    new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Alphabetizing names\n");

    System.out.println("Enter names separated by spaces:");
    StringTokenizer names1 = new StringTokenizer(key.readLine());

    int tokens = names1.countTokens();
    String[] names2 = new String[tokens];
    String y;

    for (int a = 0; a < tokens; a++) {

        names2[a] = names1.nextToken();

    }

    System.out.println("\nSorted names:");

    for (int a = 0; a < tokens; a++) {

        for (int b = a + 1; b < tokens; b++) {

            if(alphabetize(names2[a], names2[b])) {

                y = names2[a];
                names2[a] = names2[b];
                names2[b] = y;

            }

        }

    }

    for (int c = 0; c < tokens; c++) {

        System.out.println(names2[c]);

    }

}

static boolean alphabetize(String a, String b) {

    for(int c = 0; ; c++) {

        if((c == a.length()-1) && (c == b.length()-1)) {

            return false;

        }

        if(c == a.length()-1) {

            return true;

        }

        if(c == b.length()-1) {

            return false;

        }

        if((a.toLowerCase().charAt(c) - b.toLowerCase().charAt(c)) > 0) {

            return true;

        }

    }

}

Please help!! Thank you!

1
  • I'm looking at your code, but I can tell you that it has some problem also with other words Commented Apr 3, 2015 at 4:35

2 Answers 2

3

Hint 1: Look at the output. What does it look like is happening?

Hint 2: Based on the obvious conclusion from Hint 1 ... look at the alphabetize method ... and figure out why that causes what you are seeing.


Meta-hint: I think your problem here is that you don't have a consistent mental model of what alphabetize is supposed to be doing; i.e. the intended meaning of the result. There are two reasons for this:

  1. The name of the method is opaque. The word "alphabetize" is not a verb whose meaning maps to the action you are attempting to perform. The http://www.thefreedictionary.com/alphabetize link says:
alphabetize (ˈælfəbəˌtaɪz) or alphabetise. vb (tr)
1. to arrange in conventional alphabetical order
2. to express by an alphabet

Your method is doing neither of those things.

Yes ... method names are important.

  1. You don't have any comments to explain what the method should return. It is considered best practice to write a javadoc comment for any non-trivial method that forms the "contract" between the method and the code that calls the method. In this case, you need a comment that says something like "this method returns true if X, Y or Z, and false otherwise".
Sign up to request clarification or add additional context in comments.

1 Comment

Great :-) i like answers that also try to reflect thinking (methodology)
0

The problems are in alphabetizer:

  1. First if: if a and b have the same length, you don't compare their last characters.
  2. Second and Third if: if a and b have not the same length, you dont consider the last char of the shortest
  3. Fourth if: you only consider the case where the char read from a is higher than the char read from b, you should also consider the case where b is higher than a.

Tip: The loop should continue only if the char that you read are equals,


Here how to solve your problem:

static boolean alphabetize(String a, String b) {
    char ca;
    char cb;
    for(int c = 0; ; c++) {
        ca = a.toLowerCase().charAt(c);
        cb = b.toLowerCase().charAt(c);
        if((c == a.length()-1) && (c == b.length()-1)) {
            return (ca - cb) > 0;
        } else if(c == a.length()-1) {
            return false;
        } else if(c == b.length()-1) {
            return true;
        } else if (ca - cb != 0) { //if are not equals
            return (ca - cb) > 0;
        }      
    }
}

I've tryed with many strings, for example this:

aaa ccc arc abr ald old mal ald aaaa bbbb aaaa car cro arc dsjd qhjk hdjgsdaj asidasiodu asi

2 Comments

I don't think that it is helpful to rewrite a beginner's code for him / her. The chances are that he / she won't understand the code. IMO, it is much better to help the beginner to solve the problem for him / herself. That is harder to do ...
I didn't wanted to just rewrite code, indeed I was also writing an explaination of which were the problems. I think that knowing what is wrong and an example of a possible solution (if understood) can be constructive too. Anyway I agree with you and I really like your answer. Thanks for your comment, I will consider it the next times.

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.