2

I am new with Java. I try to do one of my assignment, but i can not figure out why my result still can not sort.

I have a prompt value(argument) 20 10 30 60 55, and i want to sort it. I wrote two loops, and converted prompt value (which is string) to Integer.

Result: ( It is not sorted)

20 10 30 60 55 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at question2.SortedArray.main(SortedArray.java:29)

This is the code i wrote:

int temp = 0;
    int array[] = null;
    int array2[];


    for(int i=0; i<args.length; i++){
        int a = Integer.parseInt(args[i]);
         array = new int[a];
        for(int j=0; j<args.length; j++){
            int b = Integer.parseInt(args[i]);
            array2 = new int[b];
            if(array[i]>array2[j])
                temp = array2[j];
                array2[j] = array[i];
                array[i] = temp;


        }
    }
    for (int i = 0; i < array.length; i++)
    {
              System.out.println(args[i].toString());
    }

I could understand this code i fould online below

int tempVar;
        for (int i = 0; i < numbers.length; i++)
        {
                 for(int j = 0; j < numbers.length; j++)
                 {
                         if(numbers[i] > numbers[j])
                         {
                         tempVar = numbers [j ];
                         numbers [j]= numbers [i];
                         numbers [i] = tempVar;
                         }
                 }
        }
        for (int i = 0; i < numbers.length; i++)
        {
                  System.out.print(numbers[i]+" ");
        }

}
3
  • What if you placed the elements in a List and then sorted? Commented Oct 23, 2014 at 16:59
  • 1
    Hi Chris, haven't go that far yet, because the class only teach us about Array yesterday. I wondering if i have use simple array with two loops. Commented Oct 23, 2014 at 17:04
  • 2
    Nice. If you want try and get ahead a little, you can read about Lists.. Here is the API for a List docs.oracle.com/javase/7/docs/api/java/util/List.html Commented Oct 23, 2014 at 17:09

3 Answers 3

4

First, convert the String array of args into an int array of values. Then sort and display values. You've been sorting arrays (sized by your array int value), then printing the arguments (which you didn't sort). So, something like

int[] values = new int[args.length];
for (int i = 0; i < args.length; i++) {
    values[i] = Integer.parseInt(args[i]);
}
int temp = 0;
for (int i = 0; i < values.length - 1; i++) {
    for (int j = i + 1; j < values.length; j++) {
        if (values[i] > values[j]) {
            temp = values[j];
            values[j] = values[i];
            values[i] = temp;
        }
    }
}
System.out.println(Arrays.toString(values));
Sign up to request clarification or add additional context in comments.

Comments

1

You need to initialize your array to hold your prompt values

int [] yourArray = {2,3,4 5,};

You don't need a second array to sort the values just a temporary int to hold the value that is being moved.

The statements below if needs to be enclose with { } otherwise all it will do is run to the first semicolon and then get out of the if.

Basically what the code you pasted in the second part is checking if element at the value i is greater than the value at element j. If the value is greater it swaps j with i.

for (int i = 0; i < numbers.length; i++)


  {
             for(int j = 0; j < numbers.length; j++)
             {
                     if(numbers[i] > numbers[j]) //if element at i is greater than element a j
                     {
                     tempVar = numbers [j ]; //store the number at element j in temp
                     numbers [j]= numbers [i];// set the number at element i to element j
                     numbers [i] = tempVar;// set the number at temp to element 
                     }
             } // does this for each element until no element i greater than j
    }

1 Comment

Thank you, I really don't need a second array to sort
1

Get the integers to an array at first.

 int[] yourArray = { 20, 10, 30, 60, 55 };
        for (int i = 0; i < yourArray.length; i++) {
            for (int j = 0; j < yourArray.length - 1; j++) {
                // swap
                if (yourArray[i] < yourArray[j]) {
                    int temp = yourArray[i];
                    yourArray[i] = yourArray[j];
                    yourArray[j] = temp;
                }
            }
        }
        for (int i : yourArray)
            System.out.println(i);

Output

10 20 30 55 60

2 Comments

Yes, Nabin. This is what i have been doing, but its not working. I know somewhere i did it wrong with converting from String to int.
Aren't you getting values to array?

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.