Page 2 of 2 FirstFirst 12
Results 21 to 38 of 38
  1. #21
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Yes, that is what the count is intended to do, get the result from the iterating and then add it to another arraylist/array, and then the iterator look trough again.
    What is the correct way to increment the count from time to time?

    The count is not adding what it find into the arraylist2, it only return 1
    Thank you for your time!

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    then add it to another arraylist/array
    What is this arraylist for?
    Can you write out your design/the steps for how this method is supposed to work? See my post#20 for my ideas on how it should work.

    What is the correct way to increment the count from time to time?
    Just the way you are doing it: count++;

  3. #23
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Yes, all the count is intended to do is get the result from the iterating and add it to another arraylist/array. How can I increment the count?
    now it only return even though it should have returned more than that. The count can't add the result.

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    add it to another arraylist/array.
    What is the purpose of this other arraylist?

    Do you have a design for how the method is supposed to work? You keep talking about using another arraylist but do not say what that arraylist is to be used for.

    now it only return even though it should have returned more than that
    the method returns an int. What more can it return?

  5. #25
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    The purpose for the arraylist2 is to store the result from count. When the iterator has found the month I am looking for I want that to get into count, and then count add it to arraylist2.
    Then a new iterator trough arraylist2 where the result will be counted and returned.
    Yes, the method return an int, but if it had worked as I want it to do it would have returned all who has joined in the particular month I insert into the parameter of the method

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    I still don't understand what you put into the second arraylist.
    Can you explain what you put in it and for what purpose?
    What is the value of count when you put it in the second arraylist?

    Let me rephrase what you seem to be saying. You count up the number of members that joined in a specific month by iterating thru the list of members and counting them if the month matches, and then put that count (the total number of members that joined) into the second arraylist.
    Then you get the value that you just put into the second arraylist and return it.
    The use of the second arraylist seems useless and redundant here. Why not just return the count?

  7. #27
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Hello norm

    Do you mean that I don't need an arraylist2 to store the values from the first iteration?
    Lets say that the first iteration find a match, where can I store that match so the next iteration can go on to see if it find one more match.

    If I just return the count it will only return 1, it will not sum up how many matches there really are

    Nothing are being added to the arraylist2 since the month.add(count) don't work

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    Do you mean that I don't need an arraylist2
    That is correct. You do not need the second arraylist
    What does the first iteration do? What is the value of count at the end of the iteration?
    Why is the second iteration inside of the first iteration loop?

    Add some printlns to the code to show the value of count everytime it changes
    System.out.println("count=" + count);
    and to show the value of sum everytime it changes.
    System.out.println("sum=" + sum);

  9. #29
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Java Code:
    I can't understand that I don't need somewhere to store the value from the iteration?
    Now it will only return 1, even though I now it should return 3 since there are three members who have joined in the month I am looking for
    What I am I doing wrong? 
       public int joinedInMonth(int month)
        {
            int joined = month;
            if(joined < 1 || joined > 12)
              {
                System.out.println("Not a valid month");
                }
          else
             {   
               int count = 0;
              System.out.println("count 1 =" + count); // Here the count is 0. Of course
            //int countMount 
            ArrayList nmonth = new ArrayList();
            Iterator it = club.iterator();
            while(it.hasNext()) 
            {
               Membership membership = (Membership) it.next();
            System.out.println("count 2 =" + count);// Here the count is 0
               if(joined == membership.getMonth())
                 {
                  System.out.println("count 3 =" + count);// Here the count is 0
                 //System.out.println(count);
                    count++; 
                  System.out.println("count 4 =" + count); // Here the count is 1
                  //System.out.println("sum=" + sum);
              //   nmonth.add(count);
                }
                return count;

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    What printed out from the printlns I asked you to add to your code? Copy and paste the output here.
    How many times did the loop for the first iterator loop? It should loop once for each member. Your comments say it only looped once. Why is that? Did you return before you had looped thru ALL of the members?

    You need to step thru the logic of your loops and make sure they execute as you want them to.

  11. #31
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Norm please don't be tired of me. there is something I can't figure out
    But here is the output
    Java Code:
    count 1 =0
    count 2 =0
    count 3 =0
    count 4 =0
    count 5 =0
    count 2 =0
    count 3 =0
    count 4 =0
    count 5 =0
    count 2 =0
    count 3 =0
    count 4 =0
    count 5 =0
    count 2 =0
    count 5 =0
    count 6 =0
    
    The code
         public int numberOfMembers()
        {
           return club.size();
        }
    ///////////////////////////////////////////////////////////////////////////////////    
        public int joinedInMonth(int month)
        {
            int count = 0;
            int joined = month;
            if(joined < 1 || joined > 12)
              {
                System.out.println("Not a valid month");
         }else{   
               
              System.out.println("count 1 =" + count); // Here the count is 0. Of course
           
            ArrayList nmonth = new ArrayList();
            Iterator it = club.iterator();
            while(it.hasNext()) 
            {
               Membership membership = (Membership) it.next();
            System.out.println("count 2 =" + count);// Here the count is 0
               if(joined == membership.getMonth())
                 {
                  System.out.println("count 3 =" + count);// Here the count is 0
                 
                     
                  System.out.println("count 4 =" + count); // Here the count is 0
                }
                //count++;
                System.out.println("count 5 =" + count); // Here the count is 1
               // return count;
            }
           // count++;
            System.out.println("count 6 =" + count); // Here the count is 1
           } 
           count++;
           return 0;
         }

  12. #32
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    Why does the method return a 0 on the last line?

    Here again is a brief description of how the method should work.
    Set count to 0
    Count the number of .... and add 1 to count for each occurance
    return the value of count

    Your output shows that count never changes. How is that possible?
    Why have you commented out all the calls to count++;


    Why do you have so many lines commented out?

  13. #33
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Hello Norm
    It seem to work now
    The output
    count 5 =1
    count 5 =2
    count 5 =3
    count 5 =3 Why is three being counted twice aand the same with 5, any idea?
    count 5 =4
    count 5 =5
    count 5 =5
    The return is working now, it is returning the correct number. What do you think about the counting above?

    Java Code:
      public int joinedInMonth(int month)
        {
            int count = 0;
            int joined = month;
            if(joined < 1 || joined > 12)
              {
                System.out.println("Not a valid month");
         }else{   
               
           // System.out.println("count 1 =" + count); // Here the count is 0. Of course
           
            ArrayList nmonth = new ArrayList();
            Iterator it = club.iterator();
            while(it.hasNext()) 
            {
               Membership membership = (Membership) it.next();
              // System.out.println("count 2 =" + count);// Here the count is 0
               if(joined == membership.getMonth())
                 {
                     count++;
              //  System.out.println("count 3 =" + count);// Here the count is 0                 
                }
                //count++;
               System.out.println("count 5 =" + count); // Here the count is: 1, 2, 3, 3, 4, 4, 5
             // return count;
            }
           // count++;
           // System.out.println("count 6 =" + count); // Here the count is 0
           } 
          // count++;
          // System.out.println("count 7 =" + count);
           return count; //The return work it return exactly the correct number of
                         // peolpe who joined in the month number I am inserting into the parameter
         }

  14. #34
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    Why is three being counted twice aand the same with 5, any idea?
    They are not being counted twice they are being printed twice.
    The change of count would be printed by this line:
    System.out.println("count 3 =" + count); // show count when it is changed

  15. #35
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    The output

    count 3 =1
    count 3 =2
    count 3 =3


    Thanks for all your help Norm. Can't understand why I thought that I needed two arraylist

  16. #36
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    Glad you got it working.

  17. #37
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Hello Norm
    Can I bother you with another method I can't understand?

  18. #38
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    Start another thread if you have a question on a new topic.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. how to count number of pages printed?
    By absmarty in forum New To Java
    Replies: 10
    Last Post: 01-31-2012, 06:20 PM
  2. How to count number of palindrome in a string??
    By i4ba1 in forum Advanced Java
    Replies: 5
    Last Post: 04-12-2011, 12:58 PM
  3. day number count
    By droidus in forum New To Java
    Replies: 14
    Last Post: 03-23-2011, 10:15 PM
  4. Array count number Occurances
    By gwithey in forum New To Java
    Replies: 2
    Last Post: 04-17-2009, 08:34 PM
  5. count occurence of word in a line of text
    By sinyi88 in forum New To Java
    Replies: 19
    Last Post: 02-28-2009, 07:37 AM

Tags for this Thread

Posting Permissions

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