Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Counting element(s) appearance(s) in arrays

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

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

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    Any pointers please?

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

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

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    Last edited by Norm; August 19th, 2012 at 07:30 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    jps (August 19th, 2012)

  7. #6
    Junior Member
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  8. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

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

    Quote Originally Posted by alan55 View Post
    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.

Similar Threads

  1. Counting a total and printing value from 2 arrays
    By stewart86 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 2nd, 2012, 07:25 AM
  2. Counting Matches in Parallel Arrays
    By dx8292 in forum Object Oriented Programming
    Replies: 3
    Last Post: February 1st, 2012, 09:45 AM
  3. Gradual appearance of Rectangle...
    By rdheepan in forum Web Frameworks
    Replies: 5
    Last Post: January 13th, 2012, 12:07 AM
  4. Help with Arrays - Counting elements
    By ShakeyJakey in forum Collections and Generics
    Replies: 7
    Last Post: August 8th, 2010, 04:09 PM
  5. Question: counting
    By miss confused in forum Java Theory & Questions
    Replies: 2
    Last Post: July 30th, 2010, 05:38 PM