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)

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 16
at ArrayProcessing.countAndDisplay(ArrayProcessing.java:135)

The code at line 135 uses as index value that is past the end of the array.
Look at the code and see why the value of index gets past the end of the array.

Add printlns to print out the index's value if you can not see what is happening.

how would i add printlns to print out the index's value? i never done that before

System.out.println("index=" + index);  // Show the value

Replace index in the above code with the names of the variables that you want to print out.

i still don't get the error..... this is my first time taking java class so i don't know what i am looking for.. :(

What did you print out? Can you post it?

i have to pass multiple tests and i'll post 2 main tests.

if (!file.exists())
        {
            System.out.println("There is no input file called " + "\"" + fileName + "\".");
            System.exit(0);
        }                     // <--- Line 16 in my code
        int[] data;
        data = inputData(file);
        System.out.println();
        System.out.println("Original array:");
        printArray(data);
        countAndDisplay(data);       //  <---- line 53 in my code
        System.out.println("\nDone.");

    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 = 1; ndx < array.length; ndx++)
        {
            int temp = 1;
            while (array[ndx] == current)  //  <--- line 156 in my code
            {
                ndx++;
                temp++;
            }
            System.out.printf("%5d %6d\n", current, temp);
            current = array[ndx];
        }
    }

Original array:
-12 3 -12 4 1 1 -12 1 -1 1
2 3 4 2 3 -12
My output
Count statistics:
N Count
4 2
3 3
2 2
1 4
-1 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 16
at ArrayProcessing.countAndDisplay(ArrayProcessing.java:156)
at ArrayProcessing.main(ArrayProcessing.java:53)

It should be
Count statistics:
N Count
4 2
3 3
2 2
1 4
-1 1
-12 4

second test

Original array:
156 23 19 13 8 7 5 4 2

My output
Count statistics:
N Count
156 1
23 1
19 1
13 1
8 1
7 1
5 1
4 1

And this one should be

Count statistics:
N Count
156 1
23 1
19 1
13 1
8 1
7 1
5 1
4 1
2 1

What are the names of the variables that you print out?
I see lots of numbers but no identifying labels that says what the values are.
My suggestion was to label every value that is printed like in the following:
System.out.println("index=" + index);

Where do you print the value of the array index that is going out of bounds?

i found out why it is not working about i don't know how to fix it.... since the while loop tries to check array[16] even though it doesn't exists.
how do i make sure the while loop doesn't try to access an index that's out of bounds??

how do i make sure the while loop doesn't try to access an index that's out of bounds??

One way would be to use an if statement to test the index's value
Another way would be not to increment the index's value past the end.
Another way would be to exit the loop before the index's value got too high

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.