I'm just learning java and this is my first sort program. I can't get it to run. I sure could use some help.

public class SortCharArray
{
    public static void main(String[] args) throws Exception
    {
        char[] someChars = new char[10];
        int x;



        for(x = 0; x < someChars.length; ++x)
        {
            System.out.print("Enter a character ");
            someChars[x] = (char)System.in.read();
            System.in.read(); System.in.read();

        }

        System.out.println("Before sort");
        for(x = 0; x < someChars.length; ++x)
         System.out.print(someChars[x] + " ");

        bubbleSort(someChars, someChars.length);

        System.out.println("\nAfter sort");
        for(x = 0; x < someChars.length; ++x)
         System.out.print(someChars[x] + " ");
         System.out.println();    

    }     

        public static void bubbleSort(char[] array, int len)
    {
        int a, b;
        char temp;
        int highSubscript = len - 1;
        for(a = 0; a < highSubscript; ++a)
           {
            for(b = 0; b < highSubscript; ++b)
              if(array[b] > array[b = 1])
              {
                  temp = array[b];
                  array[b] = array[b + 1];
                  array[b + 1] = temp;
              }
            }
    }        

}

Recommended Answers

All 3 Replies

Well, supplying the errors you are getting would be helpful.

Please use code tags when posting code. Read the information in the links below.

This is how I code it in c/c++:

for(a = 0; a < highSubscript-1; ++a)
{
    for(b = 0; b < highSubscript; ++b)
    {
            if(array[a] > array[b])
            {
                temp = array[a];
                array[a] = array[b];
                array[b] = temp;
             }
      }
}

I think you may be best off using KeyboardReader to take input.
And you definitely have to use Ancient Dragon's suggestion for the bubble sort.

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.