Results 1 to 11 of 11
  1. #1
    alan117 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default 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 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

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: counting element(s) appearance(s) in arrays

    Welcome, please use code tags when posting codes.
    Can you show the output when you run your code?

  3. #3
    alan117 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: counting element(s) appearance(s) in arrays

    thank mine0926, umm I'm not sure how to use code tags but I'll try next time.
    the output that I have managed to produce is the customerID and the TransactionValue pair, so I can produce 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

    Now I need to be able to sort though the above and print out how many times cutomerer 0 has a transaction, how many times customer1 has a transaction and so on.
    Can you help with that bit of the problem?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: counting element(s) appearance(s) in arrays

    Quote Originally Posted by alan117 View Post
    I'm not sure how to use code tags but I'll try next time.
    BB Code List - Java Programming Forum

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    alan117 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: counting element(s) appearance(s) in arrays

    mine0926 and Darryl
    Thanks for your comments.
    Here is the code below with code tags.

    At present if I run this code I get 0 printed out.
    If I set the value of count to 1 I get 1 printed out.

    So it does not seem to be counting!

    Can you see where I am going wrong?


    Java Code:
     int count=0;
    	int thisValue;
    	
    	for (index=0;index<size;index++){
    	count=0;
    
    
    	for (thisValue=0;thisValue<size;thisValue++){
    
    	if (customerID[index]==customerID[thisValue]){
    
    	count=count++;
    
    
    
    	}
    	
    
    	}
    	
    	System.out.println ("counter=  "+count);
    	}
    
    	//System.out.println ("counter= "+count);
    }
    With the above code I can produce this 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
    Last edited by alan117; 08-19-2012 at 09:30 AM. Reason: add output

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: counting element(s) appearance(s) in arrays

    Also posted at Counting element(s) appearance(s) in arrays
    and
    Counting element(s) in array(s) - Dev Shed

    @OP Please tell us where the same question is on other forums so we don't waste time working on a solved problem.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: counting element(s) appearance(s) in arrays

    First, please tell us if you posted this question in other forums.
    Second, please practice indention in your code.. I edit you code and put indent properly
    Java Code:
            int count;
            int thisValue;
    
            for (index=0;index< size;index++)
            {
                count=0;
                for (thisValue=0;thisValue<size;thisValue++)
                {
                    if (customerID[index]==customerID[thisValue])
                    {
                        count=count++;
                    }
                }
                System.out.println ("counter=  "+count);
            }
    On your problem, it might be useful if you put some System.out.println() so you can see why your program returns zero like this
    Java Code:
            int count;
            int thisValue;
    
            for (index=0;index< size;index++)
            {
                count=0;
                System.out.println("customerID[index] " + customerID[index]);            
                for (thisValue=0;thisValue<size;thisValue++)
                {
                    System.out.println("customerID[thisValue] " + customerID[thisValue]);            
                    if (customerID[index]==customerID[thisValue])
                    {
                        count=count++;
                    }
                }
                System.out.println ("counter=  "+count);
            }

  8. #8
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: counting element(s) appearance(s) in arrays

    another thing is you put count=count++
    i++ and i=i++ have different result. try it so you can see.

  9. #9
    alan117 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: counting element(s) appearance(s) in arrays

    Sorry about the multiple postings, I guess I should have realised people might belong to multiple forums.
    I was looking for advice and thought the more exposure I got, the more opinions I might receive.
    I appreciate your help and suggestions and certainly did not mean to waste your time by posting in several forums.
    Cheers

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: counting element(s) appearance(s) in arrays

    Quote Originally Posted by alan117 View Post
    clearly I need more code
    No you don't; you need to take a serious look at the Map<T, U> interface and the Collections framework in general. All that array fiddling is so Fortranesque ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: counting element(s) appearance(s) in arrays

    I think the assignment precludes using other classes. It's purpose is to teach array searching and using loops.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Arrays element Equals
    By must0995 in forum New To Java
    Replies: 8
    Last Post: 04-15-2012, 11:25 PM
  2. Arrays Counting Number of Occurrences
    By artur in forum New To Java
    Replies: 2
    Last Post: 12-11-2011, 02:07 AM
  3. Netbeans GUI appearance
    By scrappy in forum New To Java
    Replies: 0
    Last Post: 08-13-2011, 01:40 PM
  4. Replies: 4
    Last Post: 08-12-2011, 05:47 PM
  5. [java3D] set Appearance again
    By Dennis in forum Advanced Java
    Replies: 0
    Last Post: 09-12-2010, 04:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •