Page 1 of 2 12 LastLast
Results 1 to 20 of 29
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default How can I count "trues" in an array?

    I have arrays and I want to count the amount of times the boolean variable "isBooked" is true across them all.


    At the moment, I am trying to do this using a method where I enter the array name and an if statement (in conjunction with an index) will run through each variable in the array increasing another variable "count" if it is true.

    However, I am getting syntax completely wrong I think, I haven't done much work with arrays so do not understand them fully yet.



    This is my (rough) code for this:

    Java Code:
        public static void seatsBooked(AtoCSeat row){
            
            int count = 0;
            int index = 0;
            
            if (row[index].isBooked = true){
                count ++;           
            }
            index ++;
            
        }
    I would put in a limiter to stop the index increasing forever, but what am I doing wrong?


    note: AtoCSeat is the data type (object) of each variable. isBooked is declared in the objects.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: How can I count "trues" in an array?

    I believe you are looking for a for loop.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: How can I count "trues" in an array?

    Quote Originally Posted by KevinWorkman View Post
    I believe you are looking for a for loop.
    I haven't use them yet, I'll go research.

    Also, I forgot to mention, I was getting an alert in NetBeans saying "array required, but no AtoCSeat found"

  4. #4
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: How can I count "trues" in an array?

    I have just looked up for loops and replaced my if loop with it but I still get the same alert in NetBeans

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How can I count "trues" in an array?

    Could you show use the complete stack trace error and show use the line of code where it occurs?

  6. #6
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: How can I count "trues" in an array?

    I haven't actually compiled it, I was just getting an alert on the line number in NetBeans.

    I'll try that now.


    Should I code I put above work in theory then?

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How can I count "trues" in an array?

    Alerts are only alerts, not errors. If all you get is an alert, then you can ignore it! :D

  8. #8
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: How can I count "trues" in an array?

    I worked it out!!!




    I worked backwards through my program and realised I needed to put the input type of the parameter of he method as an array of AtoCSeat (AtoCSeat[]) instead of just an AtoCSeat.

    i.e.

    Java Code:
    public static void seatsBooked(AtoCSeat[] row){

  9. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How can I count "trues" in an array?

    Oh yeah! Good catch!

  10. #10
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: How can I count "trues" in an array?

    Quote Originally Posted by quad64bit View Post
    Oh yeah! Good catch!
    haha thanks



    I'm stuck on the final part of this though. I don't understand why it isn't working.

    This is the code relating to the counting of "trues":

    Java Code:
        public static int seatsBookedRow(AtoCSeat[] row){
           
            
            int count = 0;
            int index = 0;
            
            while(index < row.length){
            
            if (row[index].isBooked = true){
                count ++;           
            }
            index ++;
            
            }
            
            return count;
        }
        
        
        public static int seatsBookedTotal(){
            return (seatsBookedRow(A) + seatsBookedRow(B) + seatsBookedRow(C) + 
                    seatsBookedRow(D) + seatsBookedRow(E) + seatsBookedRow(F) + 
                    seatsBookedRow(G) + seatsBookedRow(H) + seatsBookedRow(J) +
                    seatsBookedRow(K) + seatsBookedRow(L));
            
        }

    I've been working on this program solidly for around 8 hours now so am going to leave it after this


    Can anybody see what is wrong with the above?

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,805
    Rep Power
    19

    Default Re: How can I count "trues" in an array?

    Java Code:
            if (row[index].isBooked = true){
    You're not checking the boolean there.
    You're setting it to true.

    Get rid of the '= true', since it's unecessary.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: How can I count "trues" in an array?

    I am not sure about the line

    if (row[index].isBooked = true) // whenever i see a single = sign i tend to think it's a assignment statement

    try writing it like this
    if(row[index].isBooked) // or if you still feel like using the equal sign you should write it like this:
    if(row[index.isBooked == true)

  13. #13
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: How can I count "trues" in an array?

    ah, yes!

    but it still isn't working.

    i get this error:

    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
    at BookingsSystem.seatsBookedTotal(BookingsSystem.jav a:158)
    at BookingsSystem.viewStatistics(BookingsSystem.java: 105)
    at BookingsSystem.userInterface(BookingsSystem.java:8 2)
    at BookingsSystem.main(BookingsSystem.java:11)
    Java Result: 1

  14. #14
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: How can I count "trues" in an array?

    And what code is one line 158 of BookingsSystem?

    For better help faster, you should boil your problem down to an SSCCE and post it in its entirety. Also, you should be compiling very often, not waiting until you think you have the whole program written.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #15
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: How can I count "trues" in an array?

    158 is the return statement:

    Java Code:
    return (seatsBookedRow(A) + seatsBookedRow(B) + seatsBookedRow(C) + seatsBookedRow(D) + seatsBookedRow(E) + seatsBookedRow(F) + seatsBookedRow(G) + seatsBookedRow(H) + seatsBookedRow(J) +seatsBookedRow(K) + seatsBookedRow(L));

    Netbeans is alerting me to that line too (I have made it all one line now), it says it can't find variable A, B, C, D etc...

  16. #16
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: How can I count "trues" in an array?

    I did realise I needed to add more methods (because if D is entered, this isn't an AtoCSeat for example.


    This is the total code now for this part of the program:

    Java Code:
        public static int seatsBookedRow(AtoCSeat[] row){
           
            
            int count = 0;
            int index = 0;
            
            while(index < row.length){
            
            if (row[index].isBooked){
                count ++;           
            }
            index ++;
            
            }
            
            return count;
        }
        
        
        public static int seatsBookedRow(DtoFSeat[] row){
           
            
            int count = 0;
            int index = 0;
            
            while(index < row.length){
            
            if (row[index].isBooked){
                count ++;           
            }
            index ++;
            
            }
            
            return count;
        }
            
            
        public static int seatsBookedRow(GtoLSeat[] row){
           
            
            int count = 0;
            int index = 0;
            
            while(index < row.length){
            
            if (row[index].isBooked){
                count ++;           
            }
            index ++;
            
            }
            
            return count;
        }
        
        
        public static int seatsBookedTotal(){
            return (seatsBookedRow(A) + seatsBookedRow(B) + seatsBookedRow(C) + seatsBookedRow(D) + seatsBookedRow(E) + seatsBookedRow(F) + seatsBookedRow(G) + seatsBookedRow(H) + seatsBookedRow(J) +seatsBookedRow(K) + seatsBookedRow(L));
            
        }

  17. #17
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: How can I count "trues" in an array?

    I think I may have the solution (don't know how to fix it yet, am only posting as I work it out.

    there is no such variable in this method as A, B, C etc...

    I need to link it somehow with the variables referring to the seat objects

  18. #18
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: How can I count "trues" in an array?

    ok, ive declared A, B, C etc... as static variables outside of any methods and this seems to have shut NetBeans up :P

    However I still get an error when compiling:

    Exception in thread "main" java.lang.NullPointerException
    SOME TEXT WHICH SHOULD BE PRINTING (I PUT IT HERE)
    at BookingsSystem.seatsBookedRow(BookingsSystem.java: 142)
    at BookingsSystem.seatsBookedTotal(BookingsSystem.jav a:192)
    at BookingsSystem.viewStatistics(BookingsSystem.java: 101)
    at BookingsSystem.userInterface(BookingsSystem.java:7 8)
    at BookingsSystem.main(BookingsSystem.java:25)
    Java Result: 1

  19. #19
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: How can I count "trues" in an array?

    Would it be best to attach all of my program (its not too big) to a post?

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,805
    Rep Power
    19

    Default Re: How can I count "trues" in an array?

    That's not a compilation error, that's a runtime error.
    line 142 of your BookingSystem is throwing a NullPointerException, which means you haven't initialised something on that line.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  2. URLConnection -- does it count as a "click"?
    By SnakeDoc in forum Networking
    Replies: 5
    Last Post: 05-29-2012, 04:35 PM
  3. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  4. Count lines cointaining "word" in input file
    By gwithey in forum New To Java
    Replies: 5
    Last Post: 04-02-2009, 05:23 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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