Java Beginners

Moderators: zibadian
Number of threads: 1285
Number of posts: 2739

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Counting element(s) appearance(s) in arrays Posted by alan117 on 17 Aug 2012 at 11:05 PM
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): 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




 

Recent Jobs