sab1ks
sab1ks

Reputation: 3

Loading space-separated user input to array

I'm new to Java. I've spent hours trying to figure out why this code doesn't work. It's supposed to take user input integers separated by whitespace, and load them into an int array. Then, the numbers are supposed to be sorted in descending order, and the frequency of repeated inputs has to be displayed in a separate int array.

I'm getting zeroes in the output though. Can anyone help me fix this program?

This is what I have:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int[] inputNumbers = new int[50];
    int[] ocurrences = new int[50];
    int size = 0;

    System.out.println("How many numbers to enter? (At most 50)");
    size = input.nextInt();

    System.out.println("Enter eaach of the numbers. Please put a space between each number.");
    String arrayString = input.next();
    input = new Scanner(arrayString);

    while(input.hasNextInt())
    {
        int nextNumber = input.nextInt();
        if(!isInArray(inputNumbers, size, nextNumber))
        {
            inputNumbers[size] = nextNumber;
            ocurrences[size] = 1;
            size++;
        }
        else
        {
            int index = search(inputNumbers, size, nextNumber);
            ocurrences[index]++;
        }
    }

    sort(inputNumbers, ocurrences, size);
    System.out.println("N\t\tCount");
    for(int i = 0; i < size; i++)
    {
        System.out.println(inputNumbers[i] + "\t\t" + ocurrences[i]);
    }
}

public static void sort(int[] inputNumbers, int[] ocurrences, int size)
{
    for(int current = 0; current < size; current++)
    {
        int max = current;
        for(int i = current; i < size; i++)
        {
            if(inputNumbers[i] > inputNumbers[max])
                max = i;
        }
        int temp = inputNumbers[max];
        inputNumbers[max] = inputNumbers[current];
        inputNumbers[current] = temp;

        temp = ocurrences[max];
        ocurrences[max] = ocurrences[current];
        ocurrences[current] = temp;
    }
}

public static int search(int[] array, int size, int number)
{
    for(int i = 0; i < size; i++)
    {
        if(array[i] == number)
            return i;
    }
    return -1;
}

public static boolean isInArray(int[] array, int size, int number)
{
    for(int i = 0; i < size; i++)
    {
        if(array[i] == number)
            return true;
    }
    return false;
}

}

The current output is:

How many numbers to enter? (At most 50)
4
Enter eaach of the numbers. Please put a space between each number.
5 5 4 7 8
N       Count
5       1
0       0
0       0
0       0
0       0
BUILD SUCCESSFUL (total time: 11 seconds)

It should have been something like

N    Count
8    1
7    1
5    2
4    1

Upvotes: 0

Views: 57

Answers (1)

SANN3
SANN3

Reputation: 10069

Initialize the array where we know the exact size, so it avoid unnecessary memory utilization.

Also input.next() will read only the next complete token not the entire line. That was the mistake in the code.

Finally the scanner resource was not closed.

I have refactored your code, please check it.

public static void main(String[] args) {
        Scanner input = null;
        try {
            input = new Scanner(System.in);
            int[] inputNumbers, ocurrences;
            int index = 0, size;

            System.out.println("How many numbers to enter? (At most 50)");
            size = input.nextInt();
            inputNumbers = new int[size];
            ocurrences = new int[size];

            System.out.println("Enter each of the numbers. Please put a space between each number.");

            for (int i = 0; i < size; i++) {
                int nextNumber = input.nextInt();
                if (!isInArray(inputNumbers, nextNumber)) {
                    inputNumbers[index] = nextNumber;
                    ocurrences[index] = 1;
                    index++;
                } else {
                    int oIndex = search(inputNumbers, nextNumber);
                    ocurrences[oIndex] ++;
                }
            }

            sort(inputNumbers, ocurrences);
            System.out.println("N\t\tCount");
            for (int i = 0; i < index; i++) {
                System.out.println(inputNumbers[i] + "\t\t" + ocurrences[i]);
            }
        } finally {
            input.close();
        }
    }

    public static void sort(int[] inputNumbers, int[] ocurrences) {
        int size = inputNumbers.length;
        for (int current = 0; current < size; current++) {
            int max = current;
            for (int i = current; i < size; i++) {
                if (inputNumbers[i] > inputNumbers[max])
                    max = i;
            }
            int temp = inputNumbers[max];
            inputNumbers[max] = inputNumbers[current];
            inputNumbers[current] = temp;

            temp = ocurrences[max];
            ocurrences[max] = ocurrences[current];
            ocurrences[current] = temp;
        }
    }

    public static int search(int[] array, int number) {
        int size = array.length;
        for (int i = 0; i < size; i++) {
            if (array[i] == number)
                return i;
        }
        return -1;
    }

    public static boolean isInArray(int[] array, int number) {
        int size = array.length;
        for (int i = 0; i < size; i++) {
            if (array[i] == number)
                return true;
        }
        return false;
    }

Upvotes: 1

Related Questions