# Counting element(s) appearance(s) in arrays

• August 18th, 2012, 10:36 PM
alan55
Counting element(s) appearance(s) in arrays
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 arrays generate random numbers for the customers and associated transaction values.
The number of elements in each array has an int variable called ‘size’.
I have successfully written the code that can display the customers and the transaction values, for example:
Customer ID, Transaction Value
2, 45.2
1, 55.2
3, 10.3
3, 20
0, 400.2
4, 100.30
3, 50.5

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 am trying now to write code (based on the numbers above) that produces this following output:
Customer: 2, Transactions:1
Customer: 1, Transactions: 1
Customer: 3, Transactions 3
Customer: 0, Transactons: 1
Customer: 4, Transactions: 1

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”.

My initial thoughts but I may be totally wrong, and clearly I need more code:
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); }```
I have been struggling for hours so any pointers would be most welcome- thanks
• August 19th, 2012, 02:17 AM
jps
Re: Counting element(s) appearance(s) in arrays
I see a somewhat clear description of the problem to be solved.
The next step is to decide how to solve it.
After you have a plan of attack, you can draw up pseudo code.
Pseudo code then easily translates into code.
If you follow that process you will have less trouble forming working code.

So my suggestion is to work on step 2, find the algorithm to solve the problem, and worry about the code several steps later.
• August 19th, 2012, 02:41 AM
alan55
Re: Counting element(s) appearance(s) in arrays
Thanks jps.
I have written down what I need to do, and I understand it in English, Unfortunately I do not understand it in Java!
Having written it down I have produced the above code and that compiles and produces the following output:
C:\ass3>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

• August 19th, 2012, 04:04 AM
jps
Re: Counting element(s) appearance(s) in arrays
It looks like the code is doing what you wrote it to do. Which is not what you wanted. The algorithm is not the solution to the problem.
Go back to step 2 and figure out a way to get what you want to get instead of what you have now.

Break up the problem into parts, and get the smallest part to work and add on from there. ...try to print out the desired values for just one customer, and adapt to print the others after you get it working for just one.
• August 19th, 2012, 07:21 AM
Norm
Re: Counting element(s) appearance(s) in arrays
• August 19th, 2012, 11:56 PM
alan55
Re: Counting element(s) appearance(s) in arrays
jps- Thanks for your help, I'll continue trying and eventually solve it.

I also apologise for multiple site posting my intention was only to obtain several opinions and certainly not to try waste anybodies time.
Cheers
• August 20th, 2012, 01:03 AM
jps
Re: Counting element(s) appearance(s) in arrays
Quote:

Originally Posted by alan55
jps- Thanks for your help, I'll continue trying and eventually solve it.
Cheers

I hope you get it working. Feel free to post more questions if you have any.