#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# How do I get an integer from an array of boolean ? (no. of 'true' values and no. of 'false' values)

Hello, I am struck in a place where I am not able to access the number of 'true' values from an array of boolean. Example - I have an array list of 500 booleans. Where two of them are 'true' and rest are 'false'. How to I tell computer to count the number of true values from the array list.?

Tagged:

• edited July 2015

some thing like this should do it (untested):

``````int trues = 0;

for(int i = 0; i < array.length; i++){
if(array[i]){
trues++;
}
}
``````

`trues` now holds the number of trues in the array...

• ``````/*
Massing Study

Concept - Create spaces in order to arrange them to achieve desired area requirement.
These spaces will be designed in grids. With the single click the area to be added in
total area. And by clicking on the same grid again the area will be deducted.

*/

int numCheck = 2000;
Check1[] check = new Check1[numCheck];

PFont font;
float count =0;
int mlty =1;
PImage img;
int test;
int limit = 150;
String s;
String lm = str(limit);
int mode =1;
color base = color(0, 0, 0, 40);
color chk =  color(224, 207, 158);
int count2;
int rem1;
int rem2;

void setup() {
size(800, 800);

int x=0;
int y=0;
for (int i=0; i<numCheck; i++) {

check[i] = new Check1(x, y, 20, base, chk, 2);
x+=20;

if (x>width) {
x=0;
y+=20;
}
}

textFont(font);
}

void draw() {
background(140);
for (int i=0; i<numCheck; i++) {
check[i].display();
}

fill(0, 40);
rect(575, 10, 300, 80);

totArea(600, 60);
}

void mousePressed() {
for (int i=0; i<numCheck; i++) {

check[i].update1();
count = count+1;
count2 = int(count/2000);
}
int trues = 0;
for (int i=0; i<check.length; i++) {
if (check[i].countt() == true) {
trues++;
println(rem1);
}
}
}

void keyPressed() {
if (key == 's') {
save("Residential3.jpg");
}
}
class Check1 {
int x, y;
int size;
color baseGray;
color checkGray;
boolean checked = false;
boolean uncheck = true;
int scale1;
int counter = -1;
int temp =0;

Check1(int xp, int yp, int s, color bg, color cc, int s1) {
x = xp;
y = yp;
size = s;
baseGray = bg;
checkGray = cc;
scale1 = s1;
}

void update1() {

if ((mouseX>=x)&&(mouseX<=x+size)&&(mouseY>=y)&&(mouseY<=y+size)) {
checked =! checked;
counter=-counter;
countt();
}
}

void display() {
stroke(0, 40);
keyPressed();
if (checked == true) {
fill(checkGray);
stroke(0);
strokeWeight(2);
strokeCap(MITER);
//line(x, y, x+size, y+size);
//line(x+size, y, x, y+size);

} else {
fill(baseGray);
strokeWeight(0.5);

}

rect(x, y, size, size);
}

boolean countt () {
if (counter == 1) {
return true;
} else {
return false;
}
}

void keyPressed() {
if (key == 'y') {
checkGray = color(224, 207, 158);
}
if (key == 'r') {
checkGray = color(0, 207, 158);
}
}
}
void statusBar(float x, float y, float val1, float val2, float val3) {
float map1 = map(val3, val1, val2, 0, 150);
fill(255, 40);
rect(x, y, 150, 8);
fill(196, 162, 69);
rect(x, y, map1, 8);
}

void header1 (int x, int y){
fill(0, 0, 0);
textSize(20);
text("Massing Study ", x, y, 150, 400);
}

void totArea(int x, int y){
s = str(int(count2));
fill(255, 255, 255);
textSize(15);
text("Area = " + s +" / " + lm + " Sq.M.", x, y);
statusBar(x, y+5, 0, limit, count2);
}
``````
• Tested:

``````int truth, fake = 0;
boolean[] array = {true, true, false};
void draw() {
for (int i = 0; i < 3; i++) {
if (array[i] == true)
truth +=1;
if (array[i] == false)
fake +=1;
}
println("True: " + truth + " False: " + fake);
exit();
}
``````
• Please refer the code above. (Ignore font).

Actually I am not able to reduce the count whn I remove the button.

• @infsys

can you please resend it arranged so that I will check on my system.

• Idk how to do it. I was posting a question about an import of android, then having a look of some questions to see if I can help. How did you do it? maybe I can paste the example the right way.

• or just click enter in front of any semicolon/curly brace

• Your code above works. The only thing is that you are not using the number...

You do a `println(rem1)`, `rem1` is never initialized and will always print 0 (default value). The local var `trues` is being set to the number of "selected" squares in your grid. But you never use it.

Also the `println()` should be after the for loop, or it will print teh whole sequence.

try:

``````int trues = 0;
for (int i=0; i<check.length; i++) {
if (check[i].countt()) {
trues++;

}
}
println(trues);
``````
• Here, all together:

``````/*
Massing Study (tweaked as an example)

Concept - Create spaces in order to arrange them to achieve desired area requirement.
These spaces will be designed in grids. With the single click the area to be added in
total area. And by clicking on the same grid again the area will be deducted.

*/

int numCheck = 2000;
Check1[] check = new Check1[numCheck];

PFont font;
float count =0;
int mlty =1;
PImage img;
int test;
int limit = 150;
String s;
String lm = str(limit);
int mode =1;
color base = color(0, 0, 0, 40);
color chk =  color(224, 207, 158);
int count2;
int rem1;
int rem2;
// a global holder
int totalChecked = 0;

void setup() {
size(800, 800);

int x=0;
int y=0;
for (int i=0; i<numCheck; i++) {

check[i] = new Check1(x, y, 20, base, chk, 2);
x+=20;

if (x>width) {
x=0;
y+=20;
}
}

font = createFont("ArialNarrow", 20);
textFont(font);
}

void draw() {
background(140);
for (int i=0; i<numCheck; i++) {
check[i].display();
}

fill(0, 40);
rect(575, 10, 300, 80);

totArea(600, 60);
}

void mousePressed() {
totalChecked = 0;
for (Check1 c : check) {
count = count+1;
count2 = int(count/2000);
c.update1();

// do it here
if (c.checked) {
totalChecked++;
}
}
println(totalChecked);
}

void keyPressed() {
if (key == 's') {
save("Residential3.jpg");
}
}
class Check1 {
int x, y;
int size;
color baseGray;
color checkGray;
boolean checked = false;
boolean uncheck = true;
int scale1;
int counter = -1;
int temp =0;

Check1(int xp, int yp, int s, color bg, color cc, int s1) {
x = xp;
y = yp;
size = s;
baseGray = bg;
checkGray = cc;
scale1 = s1;
}

void update1() {

if ((mouseX>=x)&&(mouseX<=x+size)&&(mouseY>=y)&&(mouseY<=y+size)) {
checked =! checked;
counter=-counter;
countt();
}
}

void display() {
stroke(0, 40);
keyPressed();
if (checked == true) {
fill(checkGray);
stroke(0);
strokeWeight(2);
strokeCap(MITER);
//line(x, y, x+size, y+size);
//line(x+size, y, x, y+size);
} else {
fill(baseGray);
strokeWeight(0.5);
}

rect(x, y, size, size);
}

boolean countt () {
if (counter == 1) {
return true;
} else {
return false;
}
}

void keyPressed() {
if (key == 'y') {
checkGray = color(224, 207, 158);
}
if (key == 'r') {
checkGray = color(0, 207, 158);
}
}
}
void statusBar(float x, float y, float val1, float val2, float val3) {
float map1 = map(val3, val1, val2, 0, 150);
fill(255, 40);
rect(x, y, 150, 8);
fill(196, 162, 69);
rect(x, y, map1, 8);
}

void header1 (int x, int y) {
fill(0, 0, 0);
textSize(20);
text("Massing Study ", x, y, 150, 400);
}

void totArea(int x, int y) {
s = str(int(count2));
fill(255, 255, 255);
textSize(15);
text("Area = " + s +" / " + lm + " Sq.M.", x, y);
statusBar(x, y+5, 0, limit, count2);
}
``````
• @_vk - thanks a lot for reply. :)