Home > Enterprise >  unable to sort or print out array
unable to sort or print out array

Time:10-27

I got a task to write the methods to 1. take information in and save it into array, 2. to copy the array(only with previously given arguments) and 3. to sort the array to be in descending order. it seems to me like i got the first 2 parts working but i have been stuck on the third method for 2 days without any progress. i am still learning java so i'm pretty sure i have made a dumb mistake somewhere. thanks in advance.

import java.util.*;
public class RevisionExercise {
    public static void main(String[] args) {
      
        int[] tempArray = new int[100];
        System.out.println("Type in numbers. Type zero to quit.");
        int amountOfNumbers = askInfo(tempArray);
       
        int[] realArray = new int[amountOfNumbers];
        copyInfo(realArray, tempArray);
      
        setArray(realArray);
        
        printArray(realArray);
    }
    public static int askInfo(int[] tempArray) {
        Scanner reader = new Scanner(System.in);
        int i;
        for (i = 0; i < tempArray.length; i  ) {
            System.out.print((i 1)   ". number: ");
          tempArray[i] = reader.nextInt();
        if (tempArray[i] == 0) {
        return tempArray[i];    
    }
    }
    return i;
}   
    
    private static int[] copyInfo(int[] realArray, int[] tempArray)
    {
    int targetIndex = 0;
    for( int sourceIndex = 0;  sourceIndex < tempArray.length;  sourceIndex   )
{
    if( tempArray[sourceIndex] != 0 )
        tempArray[targetIndex  ] = tempArray[sourceIndex];
}
    realArray = new int[targetIndex];
    System.arraycopy( tempArray, 0, realArray, 0, targetIndex );
    return realArray;
}
    
    public static int[] setArray(int[] realArray)
    {
     int temp = 0;
        for (int i = 0; i <realArray.length; i  ) {     
          for (int j = i 1; j <realArray.length; j  ) {     
              if(realArray[i] >realArray[j]) {
                 temp = realArray[i];    
                 realArray[i] = realArray[j];    
                 realArray[j] = temp;    
               }     
            }
    }  
        return realArray;
    }   

    public static void printArray(int[] realArray ) {
        System.out.println("\nOrdered array: ");
        for(int i = 0; i < realArray .length; i  ) {
            System.out.println(realArray [i]);
        }
    }

the output i'm getting looks like this.

Type in numbers. Type zero to quit.
1. number: 3
2. number: 8
3. number: 5
4. number: 6
5. number: 9
6. number: 0

Ordered array:

while it should look like this:

Type in numbers. Type zero to quit.
1. number: 3
2. number: 8
3. number: 5
4. number: 6
5. number: 9
6. number: 0

Ordered array: 
9
8
6
5
3

CodePudding user response:

You made a mistake in calculating the total numbers entered by the user. Your function always return 0. So change it to something like this:

public static int askInfo(int[] tempArray) {
    Scanner reader = new Scanner(System.in);
    int totalNumbers = 0;
    for (int i = 0; i < tempArray.length; i  ) {
        System.out.print((i 1)   ". number: ");
        int number = reader.nextInt();
        if (number != 0) {
            totalNumbers  ;
            tempArray[i] = number;
        } else {
            break;
        }
    }
    return totalNumbers;
}

Chage the sort function to this:

public static void setArray(int[] realArray) {
    int temp = 0;
    for (int i = 0; i < realArray.length; i  ) {
        for (int j = i 1; j < realArray.length; j  ) {
            // Use < for descending order and > for ascending order
            if(realArray[i] < realArray[j]) {
                 temp = realArray[i];
                 realArray[i] = realArray[j];
                 realArray[j] = temp;
            }
        }
    }
}

Your main program

int[] tempArray = new int[100];
System.out.println("Type in numbers. Type zero to quit.");
int amountOfNumbers = askInfo(tempArray);

int[] realArray = new int[amountOfNumbers];
System.arraycopy(tempArray, 0, realArray, 0, amountOfNumbers);

setArray(realArray);
printArray(realArray);

CodePudding user response:

Each of your methods returns an array but you never use this return value.

There are 2 modifications necessary to fix it:

  1. Keep the value of the method output:

    realArray=copyInfo(realArray, tempArray);

    So you put the result of the copy in the realArray variable.

  2. You expect the askInfo method to return the number of entered numbers but in fact you return the value of the last entered number.

So instead of return tempArray[i]; you have to return i;

  • Related