Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Problem Sorting Partially Filled Arrays

807599Apr 15 2007 — edited Apr 16 2007
I am having trouble sorting a partially filled array. What I want to have happen is have the user enter up to 50 numbers(int's only, 0's and negatives are allowed) and store the input in an array. Then sort the array, and display the sorted array from highest to lowest back to the user. The next step, which I haven't addressed yet is counting the number occurrences of each unique int and display that total to the user.

I have the array fill working correctly. Where I am having problems is when i try and sort the array of user entered values. Instead of only sorting the entered values, it add's "0's" to the array and sorts and displays them as well.

Here is what I want to have happen:

The user enters 1 2 3 4 3 2 1, the output should be 4 3 3 2 2 1 1. But what is actually being output is 4 3 3 2 2 1 1 0 0 0 0 0 0 etc.(until the array is full).

What am I doing wrong? If I display the contents of the array prior to sorting, it will only display the numbers entered, it does not add the extra 0's to fill the array. Any other suggestions on my code are welcome as well, always trying to get better at this, its tough teaching yourself form a book!

And I have left some of my tracer variables in there as well.
import java.util.Scanner;
import java.util.Arrays;

public class SortNumbers {
    //user can not enter more than 50 numbers to be sorted
    private static final int MAX_NUMBERS_ENTERED = 50;
    
    public static void main(String[] args) {
        // TODO code application logic here
        
        int[] sortThis = new int[MAX_NUMBERS_ENTERED];
        int numberUsed = 0;
        int count;
        System.out.println("Enter the numbers you would like to sort. Enter '1234' to stop. ");
        numberUsed = fillArray(sortThis);
        outputArray(sortThis, numberUsed);
        
    }
    
    //this receives numbers from the user
    public static int fillArray(int[] a){
        Scanner keyboard = new Scanner(System.in);
        
        int next;
        int index = 0;
        next = keyboard.nextInt();
        while ((next != 1234) && (index < a.length)){
            a[index] = next;
            index++;
            next = keyboard.nextInt();
        }    
        
        return index;
    }
    
    //this outputs the content of the array to the user
    public static void outputArray(int[] a, int numberUsed){
        //show the contents of the array
        System.out.println("The numbers you entered are: ");
        for (int i = 0; i < numberUsed; i++){
            System.out.print(a[i] + " ");
        }
        System.out.println();
        
        System.out.println("***********");
        System.out.println("array length: " + a.length + ", " + "what array length should be: " + numberUsed);
        System.out.println("***********");
        
        //sort the array and show the sorted output
        Arrays.sort(a);
        for (int i = (MAX_NUMBERS_ENTERED - 1); i >= 0; i--){
            System.out.print(a[i] + " ");
        }            
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 14 2007
Added on Apr 15 2007
13 comments
881 views