Search code examples
javaarrayssortingelementfrequency

Why is my terminal on java showing -1 as one of the values in array?


In my java program, I am trying to display a table of two single arrays in descending order. I have managed to display it in both ascending and descending order. However, there is an additional array element -1 in my terminal. The -1 can be seen in the picture attached below.

-1 element inside array

This is my attempt so far:

import java.util.*;

public class Q2_Frequency {

    public static void main(String[] args) {

        int sum = 0, mean, temp;
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter the number of days: "); //take input from user
        int n = input.nextInt();

        int a[] = new int[n];
        int b[] = new int[n];
        int c = 0;

        System.out.println("Please enter number of trucks using a road over the " + n + " day period: ");
        for (int i = 0; i < n; i++) { //input into array
            a[i] = input.nextInt();
            sum = sum + a[i];
        }
        mean = sum / n;
        System.out.println("The mean is: " + mean); // calculate mean of n day period
        System.out.println("Sorted in ascending order");
        System.out.println("Input\tFrequency");//print table in ascending order
        for(int i = 0 ; i < n ; i++)
        {
            for(int j = i + 1 ; j < n ; j++)
            {
                if (a[i] > a[j])
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            c = 1;
            if (a[i] != 1) {
                for (int j = i + 1; j < n; j++) {
                    if (a[i] == a[j]) {
                        c = c + 1;
                        a[j] = -1;
                    }
                }
                b[i] = c;
            }
        }

        for (int i = 0; i < n; i++) {
            if (a[i] != -1)
            {
                System.out.println(a[i] + "\t\t\t"+ b[i]);
            }
        }

        System.out.println("Sorted in descending order");
        System.out.println("Input\tFrequency");//print table in ascending order
        for(int i = 0 ; i < n ; i++)
        {
            for(int j = i + 1 ; j < n ; j++)
            {
                if (a[i] < a[j])
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;

                }
            }
            System.out.println(a[i] + "\t\t\t"+ b[i]);
        }

        ArrayLength(a);

    }

    private static void ArrayLength(int []array) // method to count number of inputs
    {
        if (array==null)
        {
            System.out.println("Number of input is 0.");
        }
        else
        {
            int arrayLength = array.length;
            System.out.println("Number of input is: "+arrayLength);
        }

    }
}

Does anyone have an idea why the -1 appears only in the descending order and why?


Solution

  • I refactor your code a bit but this fixes the issues you are having

    public static void main(String[] args) {
        int sum = 0, mean = 0;
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter the number of days: ");
        int n = input.nextInt();
        int[] a = new int[n];
        int[] b = new int[n];
        System.out.println("Please enter number of trucks using a road over the " + n + " day period: ");
        for (int i = 0; i < n; i++) {
            a[i] = input.nextInt();
            sum += a[i];
        }
        if (n != 0) mean = sum / n;
        System.out.println("The mean is: " + mean);
        printHeaders("Sorted in ascending order");
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                sort(a[i] > a[j], a, b, i, j);
            }
        }
        calculateFrequency(n, a, b);
        printValues(n, a, b);
        printHeaders("Sorted in descending order");
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                sort(a[i] < a[j], a, b, i, j);
            }
        }
        printValues(n, a, b);
        ArrayLength(a);
    }
    private static void calculateFrequency(int n, int[] a, int[] b) {
        for (int i = 0; i < n; i++) {
            int c = 1;
            for (int j = i + 1; j < n; j++) {
                if (a[i] != a[j]) continue;
                c = c + 1;
                a[j] = -1;
            }
            b[i] = c;
        }
    }
    private static void printValues(int n, int[] a, int[] b) {
        for (int i = 0; i < n; i++) {
            if (a[i] != -1) System.out.println(a[i] + "\t\t\t" + b[i]);
        }
    }
    private static void sort(boolean statement, int[] a, int[] b, int i, int j) {
        if (!statement) return;
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    
        temp = b[i];
        b[i] = b[j];
        b[j] = temp;
    }
    private static void printHeaders(String stringOrder) {
        System.out.println(stringOrder);
        System.out.println("Input\tFrequency");
    }
    private static void ArrayLength(int[] array) {
        System.out.println("Number of input is: " + array.length);
    }
    

    this method is for sorthing the a and b array at the same time

    private static void sort(boolean statement, int[] a, int[] b, int i, int j) {
        if (!statement) return;
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    
        temp = b[i];
        b[i] = b[j];
        b[j] = temp;
    }
    

    this one if for print the values of the 2 arrays

    private static void printValues(int n, int[] a, int[] b) {
        for (int i = 0; i < n; i++) {
            if (a[i] != -1) System.out.println(a[i] + "\t\t\t" + b[i]);
        }
    }