0

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;
              }
            }
    }        

}

Edited by happygeek: fixed formatting

4
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by kimbokasteniv
0

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;
             }
      }
}
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.