# Thread: Sorting numbers of an array in a descending order

1. Member
Join Date
Sep 2016
Posts
33
Rep Power
0

## Sorting numbers of an array in a descending order

I have wrote a program that lets the user input numbers into the array and the it is supposed to sort the list and then count and print out the numbers in descending order with the number of times that number has been output, I have everything done except getting the counter to display the number correctly
Java Code:
```public class IntList {

public static int[] getArray() {

Scanner sc = new Scanner(System.in);
int[] intArr = new int[50];
System.out.println("Enter first number: ");
int number = sc.nextInt();
while (number != 0){
++intArr[number];
System.out.println(" Enter next number(Enter 0 to exit): ");
number= sc.nextInt();

}
sc.close();
return intArr;
}

public static void printArray(int[] array){

for (int i = 0; i < array.length; i++) {
if (array[i] > 0){
if (array[i] > 1)
System.out.println(i + " occurs " + array[i] + " times");
else
System.out.println(i + " occurs " + array[i] + " time");        }
}}

public static void sortArray(int[] array){
int j, value;
for(int i=1; i < array.length; i++){
value= array[i];
for (j = i - 1;(j>=0) && (array[j] < value); j--){
array[j + 1] = array[j];
}
array[j+1] = value;
}
}

public static void main(String[] args) {

int[] myArray = getArray();
printArray(myArray);

sortArray(myArray);
printArray(myArray);
}

}```
The output of printarray is correct but its when it sorts it then tries to print it again is that it comes up with the incorrect number, instead it gives me 0,1,2, can anyone help me, assignment is due in a couple hours
Without the sort
Java Code:
```12 occurs 1 time
23 occurs 2 times
34 occurs 1 time```
With the sort
Java Code:
```0 occurs 2 times
1 occurs 1 time
2 occurs 1 time```

Another problem im having is that I want it to be no more than 50 inputs but instead it gives an error if I input a number more than 50
Last edited by dmp5658; 09-20-2016 at 02:31 AM.

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Sorting numbers of an array in a descending order

First, look at lines 26 and 28. Look carefully at what you are printing.

You need to check to ensure that no more than 50 elements are entered.

Regards,
Jim

3. Member
Join Date
Sep 2016
Posts
33
Rep Power
0

## Re: Sorting numbers of an array in a descending order

Sorry, I'm vert new at this so I don't really understand whats wrong with the print line

4. ## Re: Sorting numbers of an array in a descending order

it gives an error
Please copy the full text of the error message and paste it here. It has important info about the error.

The Arrays class's toString method is useful for printing array for debugging:
Java Code:
`  System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));`
Last edited by Norm; 09-20-2016 at 03:17 AM.

5. Member
Join Date
Sep 2016
Posts
33
Rep Power
0

## Re: Sorting numbers of an array in a descending order

Fixed my code to work with descending order, the only problem that persists now is it in still not letting me input over 50
heres the code
Java Code:
```public class IntList {

public static void main(String[] args){
Scanner input = new Scanner(System.in);

int[] array = new int[50];

System.out.println("Enter number: ");
int number = input.nextInt();

while (number != 0){
++array[number];

System.out.print(
"Enter number (zero to exit): ");
number = input.nextInt();
}
input.close();

System.out.println("Number\tCount");
for (int i = array.length-1; i >= 0 ; i--) {
if (array[i] > 0){
if (array[i] > 1)
System.out.println(i + " \t\t " + array[i]  );
else
System.out.println(i + " \t\t " + array[i] );
}
}
}
}```

This is the error
Java Code:
```Enter number:
12
Enter number (zero to exit): 12
Enter number (zero to exit): 46
Enter number (zero to exit): 51
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 51
at IntList.main(IntList.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 1```

6. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Sorting numbers of an array in a descending order

My mistake. I misread your code. The problem is with your logic. You enter 5 so array[5] = 1. You enter 5 again so array[5] = 2.
Let's say your array is now [0,0,0,0,0,2,0,0,0]. You print out "5 occurs array[5] times." But then look what happens when you sort it. Your
number count and indices no longer match up. your are sorting in reverse order so you have [2,0,0,0,0,0,0,0,0]. So 0 occurs 2 times, etc.
Even sorting in ascending order won't solve the problem. You need to rethink your algorithm.

Also, to make your code a bit tidier, please use {} for even single statements and ensure all of them align properly.

Regards,
Jim

7. ## Re: Sorting numbers of an array in a descending order

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 51
at IntList.main(IntList.java:19)
The statement at line 19 used an index past the end of the array. The code should test that the user's input is not past the end of the array before using it.

Note: Valid array indexes range from 0 to the array's length-1. Use the .length attribute to get the array's length: theArray.length

8. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Sorting numbers of an array in a descending order

Unless you are required to use arrays, a List implementation (e.g. ArrayList) is usually preferred. It provides
methods for manipulating it contents and also grows dynamically.

Regards,
Jim

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•