Quantcast
Channel: Programmers Heaven Forums RSS Feed
Viewing all articles
Browse latest Browse all 2703

Counting element(s) appearance(s) in arrays

$
0
0
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


Viewing all articles
Browse latest Browse all 2703

Trending Articles