### Thread: Counting element(s) in array(s)

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
2
Rep Power
0

#### Counting element(s) in array(s)

Hi all
I need some help please with Java and arrays.
I have two arrays: customerID and transactionValue (they are pairs so each transaction has a customer ID and a customer may have made several transactions).
The number of elements in each array has an int variable called ‘size’.

I need to work out, and display (using loops and ‘if’ statements, not allowed to create any other arrays or lists): for each customer I need to display the number of transactions that are on the list.
So I thought I could use variable (int for customer ID and double for transaction values) to “go through the array and count the number of times that for example customer 0 is represented, when that is done do the loop again and see how many times customer 1 is represented and so on”.
This is my initial thoughts but I may be totally wrong, and clearly I need more code:
int count=0;
int thisValue;

for (index=0;index<size;index++){

for (thisValue=0;thisValue<size;thisValue++){

if (customerID[index]==thisValue){

count=count++;
}

}

}

System.out.println ("The count is= "+count);
}

The final output must look something like this:

Customer ID Transaction Value
2, 45.2
1, 55.2
3, 10.3
3, 20
0, 400.2
4, 100.30
3, 50.5

Customer: 2, Transactions:1
Customer: 1, Transactions: 1
Customer: 3, Transactions 3
Customer: 0, Transactons: 1
Customer: 4, Transactions: 1

I have been struggling for hours so any pointers would be most welcome- thanks
2. Before you try writing code you should make a list of the steps (an algorithm) that the program needs to do (pseudo code). Once the logic in the list of steps is worked out, then move on to write the code.
Can you post the steps the program needs to do to solve the problem. When that is worked out, move on to writing the code.

When posting code please wrap the code it code tags to preserve it formatting.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
2
Rep Power
0
Code:
```for ( i = 0 ; i < 10 ; i++ ) {
count = 0 ;
for ( j = 0 ; j < 10 ; j++) {
if (customerID[i] == customerID[j]) { count++ ; }
}
Print customerID[i] + count here
}```
Having written it down I have produced the above code and that compiles and produces the following output:
C:\***3>java As
CustomerID, Transaction Value
3 30.31
2 13.21
0 23.09
3 48.58
1 4.12
4 38.53
2 8.83
2 9.3
4 85.21
2 72.34
customer:3, 0
customer:2, 0
customer:0, 0
customer:3, 0
customer:1, 0
customer:4, 0
customer:2, 0
customer:2, 0
customer:4, 0
customer:2, 0

What I do not understand now is why the code will not count the customers “once” and then find how many transactions they each have?

So I want it to now print:
customer:3, 2
customer:2, 4
customer:0, 1
customer:4, 2