(This is for a project, so yes it is homework) How would I finish this java code?

Posted by user2924318 on Stack Overflow See other posts from Stack Overflow or by user2924318
Published on 2013-10-27T03:44:39Z Indexed on 2013/10/27 3:53 UTC
Read the original article Hit count: 96

Filed under:
|
|

The task is to create arrays using user input (which I was able to do), then for the second part, use a separate method to sort the array in ascending order then output it. I have gotten it to do everything I need except I don't know how I would get it to sort. The directions say to use a while loop from 0 to the length to find the minimum value then swap that with the 1st, but I don't know how to do this. This is what I have so far:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int storage = getNumDigits(in);
    if(storage == 0){
        System.out.print("No digits to store? OK, goodbye!");
        System.exit(0);
    }
    int []a = new int [storage];
    a = getDigits(a, in);
    displayDigits(a);
    selectionSort(a);
}

private static int getNumDigits(Scanner inScanner) {
    System.out.print("Please enter the number of digits to be stored: ");
    int stored = inScanner.nextInt();
        while(stored < 0){
        System.out.println("ERROR! You must enter a non-negative number of digits!");
        System.out.println();
        System.out.print("Please enter the number of digits to be stored: ");
        stored = inScanner.nextInt();
        }
    return stored;
}

private static int[] getDigits(int[] digits, Scanner inScanner) {
    int length = digits.length;
    int count = 0; 
    int toBeStored = 0;
    while(count < length){
    System.out.print("Enter integer " +count +": ");
    toBeStored = inScanner.nextInt();
    digits[count] = toBeStored;
    count++;
    }
    return digits;
}

private static void displayDigits(int[] digits) {
    int len = digits.length;
    System.out.println();
    System.out.println("Array before sorting:");
    System.out.println("Number of digits in array: " +len);
    System.out.print("Digits in array: ");
    for(int cnt = 0; cnt < len-1; cnt++){
        System.out.print(digits[cnt] + ", ");
    }
    System.out.println(digits[len-1]);
}

private static void selectionSort(int[] digits) {
    int l = digits.length;
    System.out.println();
    System.out.println("Array after sorting:");
    System.out.println("Number of digits in array: " +l);
    System.out.print("Digits in array: ");
    int index = 0;
    int value = digits[0];
    int indVal = digits[index];
    while(index < l){
        indVal = digits[index];
        if(indVal <= value){
            indVal = value;
            digits[index] = value;
            index++;
        }
        else if(value < indVal){
            index++;
        }
        System.out.print(value);
        //This is where I don't know what to do.
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays