Hi i need to make a method that does the example below
if the array contains these number
-12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12
The output should be:
N Count
4 2
3 3
2 2
1 4
-1 1
-12 4
public static void countAndDisplay(int[] array) throws IOException
{
System.out.println("Count statistics:");
System.out.println(" N Count");
int current = array[0];
for (int ndx = 0; ndx < array.length; ndx++)
{
int temp = 1;
while (array[ndx] == current)
{
temp++;
ndx++;
}
System.out.printf("%5d %6d\n", current, temp);
current = array[ndx];
}
}
however my out come is
Count statistics:
N Count
4 2
3 2
2 1
1 3
-1 0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 16
at ArrayProcessing.countAndDisplay(ArrayProcessing.ja va:135)
at ArrayProcessing.main(ArrayProcessing.java:32)