Question: Somehow I keep getting array out of bound everytime I want to input negative integers. Such as -12 and -1 in the example output. import

Somehow I keep getting array out of bound everytime I want to input negative integers.

Such as -12 and -1 in the example output.

import java.util.Scanner; public class IntList { public static void main(String[] args) { int array[] = new int[50]; int userInput; int arraySize = 0; System.out.println("This is a occurence calculator"); Scanner keyboard = new Scanner(System.in); System.out.println("Enter an integer, or 0 to quit: "); userInput = keyboard.nextInt(); while (userInput != 0) { array[arraySize] = userInput; System.out.println("Enter an integer, or 0 to quit: "); userInput = keyboard.nextInt(); arraySize++; } //Arrays.sort(array); bubbleSort(array); int len = array[array.length - 1] + 1; int occurCount[] = new int[len]; for (int n : array) { occurCount[n]++; } System.out.println("N |Counts"); for (int j = 1; j = 1) { System.out.println(j + "\t\t" + "|" + occurCount[j]); } } } private static void bubbleSort(int[] intList) { boolean didSwap = false; do { didSwap = false; for (int i = 0; i  intList[i + 1]) { // SWAP int temp = intList[i]; intList[i] = intList[i + 1]; intList[i + 1] = temp; didSwap = true; } } } while (didSwap); } } 

Somehow I keep getting array out of bound everytime I want to

Write a program that reads numbers from the keyboard into an array of type int You can assume that type there won't be more than 50 elements added to the list. Your output should be a two column list. The first is the set of distinct array elements, the second column is the number of occurrences of each element. The list should be sorted on entries in the first column, largest to smallest. As an example for the array itself: -12, 3,-12, 4, 1, 1,-12, 1,-1, 1, 2, 3, 4, 2, 3,-12 Your output should look like the following: Count 12

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!