I'm trying to alphabetize an array by comparing the characters at position 0 of two strings in an array, but I don't understand why when the if statement doesn't run my trail variable doesn't get incremented.

//alphabetize array

public class AlphaArray
{
    public static void main(String[] args)
    {
        String[] yourChoiceItems =
                           {"Blueberry Muffins   0.85",
                            "Strawberry Bagels   0.80",
                            "Lite Yogurt         0.75",
                            "Vanilla Ice Cream   2.75",
                            "Hash Browns         2.50",
                            "Toast               2.00",
                            "French Fries        1.50",
                            "Onion Soup          3.00",
                            "Coffee              0.90",
                            "Iced Tea            1.00",
                            "Hot Chocolate       1.75"};
        for(int i=0; i<yourChoiceItems.length-1; i++)
        {
            String trail = yourChoiceItems[i];//initially yourChoiceItems[0]
            String current = yourChoiceItems[i+1];//initially yourChoiceItems[1]
            String temp;//temporary string for if they need to be swapped

            System.out.println("i is: "+i+"trail is: "+trail);

            if(trail.charAt(0)>current.charAt(0))
            {
                temp = trail;
                yourChoiceItems[i] = current;
                yourChoiceItems[i+1] = temp;    
            }
        }

    }
}

I originally tried just using the compareTo method for the String class, but I had the same bug.

when the if statement doesn't run my trail variable doesn't get incremented.

Where do you assign any value to the trail variable inside the if statement? If you don't assign it a value its value does not change.

Can you post the code's output and add some comments to it describing what you are talking about?

Try debugging by adding a println that prints out the value of current when a swap is made.

Edited 4 Years Ago by NormR1

I also noticed that the bug is when the if statement does execute

I have a print line that prints i's value (the number of loops) and trail's value. If you run the code I posted you can see the output.

Try more debugging to see what is happening. Print out the full contents of the array after every time it is changed. Use the Arrays class's toString method:
System.out.println("an ID "+ java.util.Arrays.toString(theArrayNameHere));

Edited 4 Years Ago by NormR1

Well just glancing at it, this is what it appears to be doing.
It leaves Blueberry; then swaps Lite and Strawberry.

It compares Vanilla to Lite and swaps it, then swaps places with Hash and so on. In other words; the V in Vanilla goes to the bottom of the list whereupon it disappears (!) {Try adding an "X" to the list and Vanilla will reappear. I'll leave you to work out why.

The for loop only traverses the array once, and the work is nearly all comparing 'V' to the rest.

Try putting in some more print statements to keep track. At least print the array out at the end to see what is achieved.

Edited 4 Years Ago by Starstreak

This article has been dead for over six months. Start a new discussion instead.