Results 1 to 8 of 8
  1. #1
    dmp5658 is offline Member
    Join Date
    Sep 2016
    Posts
    33
    Rep Power
    0

    Default 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. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    dmp5658 is offline Member
    Join Date
    Sep 2016
    Posts
    33
    Rep Power
    0

    Default 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. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    dmp5658 is offline Member
    Join Date
    Sep 2016
    Posts
    33
    Rep Power
    0

    Default 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. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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
    If you don't understand my response, don't ignore it, ask a question.

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

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 11-13-2011, 05:17 PM
  2. Sorting in ascending and descending order
    By flpanthers1 in forum New To Java
    Replies: 10
    Last Post: 06-27-2011, 03:48 PM
  3. Replies: 3
    Last Post: 01-13-2011, 06:07 PM
  4. [SOLVED] Sorting array in descending order?
    By dan0 in forum New To Java
    Replies: 14
    Last Post: 04-16-2009, 12:19 AM
  5. Sorting in descending order
    By santanu in forum New To Java
    Replies: 6
    Last Post: 11-26-2008, 11:43 PM

Posting Permissions

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