Results 1 to 20 of 29
- 11-06-2012, 08:33 PM #1
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
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:
I would put in a limiter to stop the index increasing forever, but what am I doing wrong?Java Code:public static void seatsBooked(AtoCSeat row){ int count = 0; int index = 0; if (row[index].isBooked = true){ count ++; } index ++; }
note: AtoCSeat is the data type (object) of each variable. isBooked is declared in the objects.
- 11-06-2012, 08:36 PM #2
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!
- 11-06-2012, 08:44 PM #3
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
- 11-06-2012, 08:49 PM #4
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
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
- 11-06-2012, 09:08 PM #5
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?
- 11-06-2012, 09:25 PM #6
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
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?
- 11-06-2012, 10:14 PM #7
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
- 11-06-2012, 10:21 PM #8
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
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){
- 11-06-2012, 10:31 PM #9
Re: How can I count "trues" in an array?
Oh yeah! Good catch!
- 11-06-2012, 11:23 PM #10
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Re: How can I count "trues" in an array?
haha thanks
.gif)
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-07-2012, 09:52 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,466
- Rep Power
- 16
Re: How can I count "trues" in an array?
You're not checking the boolean there.Java Code:if (row[index].isBooked = true){
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.
- 11-07-2012, 10:02 AM #12
Member
- Join Date
- Nov 2012
- Location
- Johannesburg, South Africa
- Posts
- 39
- Rep Power
- 0
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)
- 11-07-2012, 02:48 PM #13
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
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
- 11-07-2012, 03:13 PM #14
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!
- 11-07-2012, 03:49 PM #15
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
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...
- 11-07-2012, 03:51 PM #16
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
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)); }
- 11-07-2012, 03:59 PM #17
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
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
- 11-07-2012, 04:05 PM #18
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
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
- 11-07-2012, 04:09 PM #19
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
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?
- 11-07-2012, 04:28 PM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 10,466
- Rep Power
- 16
Similar Threads
-
Convert string operation symbols "+", "-", "/", "*" etc.
By Googol in forum New To JavaReplies: 3Last Post: 10-30-2012, 03:06 PM -
URLConnection -- does it count as a "click"?
By SnakeDoc in forum NetworkingReplies: 5Last Post: 05-29-2012, 04:35 PM -
loop "play again" in an 8 ball game , loops but wont let me answer my "out.print"
By IareSmart in forum New To JavaReplies: 1Last Post: 02-01-2012, 08:37 PM -
Count lines cointaining "word" in input file
By gwithey in forum New To JavaReplies: 5Last Post: 04-02-2009, 05:23 AM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks