codeRed codeRed - 7 days ago 3
Java Question

Counting occurences of integers in a java array

I am trying to count occurrences of integers in an array. I would like to be able to modify the following loop that counts characters...

for (int i = 0; i < chars.length; i++)
counts[chars[i] - 'a']++;


I tried modifying it like this but getting an ArrayIndexOutOfBoundsException error...

for (int i = 0; i < ints.length; i++)
counts[ints[i] - '0']++;


Any ideas?

I have searched through many questions with similar titles but not in the fashion I need.

UPDATED

Here is my updated code. The user enters integers numbered 1 through 25. The program is supposed to count the occurrences of those integers using another array. I updated the code using...

counts[vals[i] - 1]++;


Which allows the program to compile. However, all the values of count are zeros.

UPDATED CODE...

import java.util.Scanner;


class CountIntOccurs
{
private static final int MAX_NUM_OF_VALUES = 200;

public static void main ( String [] args )
{
Scanner in = new Scanner( System.in );
int[] vals = new int[MAX_NUM_OF_VALUES];
int numOfVals = 0;

// Read and store values
System.out.println( "Enter integers <= 25, one per line, hit control-Z when done: " );
while ( in.hasNextLine() )
{
vals[numOfVals] = Integer.parseInt( in.nextLine() );
++numOfVals;
}

// Close application if no values entered
if ( numOfVals == 0 )
System.exit( 0 );


// declare counts array
int[] counts = new int[25];


System.out.println( counts.length );

//??????? For each integer in the array, count it
for (int i = 0; i < counts.length; i++){
counts[vals[i] - 1]++;
}



//display values enetered
for ( int i = 0; i < numOfVals; ++i )
{
System.out.println( vals[i] );
}


//?????? display counted integers????
for ( int i = 0; i < counts.length; ++i )
{
System.out.println( counts[i] );
}


}
}


Right now , what i need is to display the occurrences of the integers that are input. If I type in 1 1 2 2 2 the results should be 2 3 0 0 0 ... 0 .

Answer
for (int i = 0; i < counts.length; i++) {
    counts[vals[i]-1]++; 
}

This is the problem, because you have counts.length instead of numOfVals. The loop is supposed to increase the count of some number by 1 for each entered value, so you have to keep looping until you have counted every entered value.

If you use counts.length, you will get ArrayIndexOutOfBoundsException if numOfVals is smaller than 25 (because counts.length is initialized to 25). If numOfVals is bigger than 25, you will only count the first 25 inputs.

The solution is therefore:

for (int i = 0; i < numOfVals; i++) {
    counts[vals[i]-1]++; 
}

Now, you are printing every number on a new line by using println() instead of print(), and you aren't printing any text, so it is hard to interpret the output of the program. I made some superficial changes to the program to make it easier to understand. Here's the complete program:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int[] vals = new int[MAX_NUM_OF_VALUES];
    int numOfVals = 0;

    System.out.println( "Enter integers <= 25, one per line, hit control-Z when done: " );
    while (in.hasNextLine()) {
        vals[numOfVals] = Integer.parseInt(in.nextLine());
        ++numOfVals;
    }
    if(numOfVals == 0)
        System.exit(0);

    int[] counts = new int[25];

    //changed counts.length to numOfVals
    for (int i = 0; i < numOfVals; i++) {
        counts[vals[i]-1]++; 
    }

    //display values entered
    System.out.print("Entered values: ");
    for (int i = 0; i < numOfVals; ++i) {
        System.out.print(vals[i]);
    }
    System.out.println(); //new line

    //display counted integers
    System.out.print("Counts of the values: ");
    for (int i = 0; i < counts.length; ++i) {
        System.out.print(counts[i]);
    }
}

Note that if the user enters more than 200 values, you will get an ArrayIndexOutOfBoundsException as well, because MAX_NUM_OF_VALUES is 200, but you have no way of actually enforcing that.