# Check boolean method in all instances for true/false

**URL:** <https://discourse.processing.org/t/check-boolean-method-in-all-instances-for-true-false/18022>\
**Category:** Coding Questions\
**Created:** [February 22, 2020, 7:43pm UTC](https://discourse.processing.org/t/check-boolean-method-in-all-instances-for-true-false/18022 "2020-02-22T19:43:17Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [February 22, 2020, 7:43pm UTC](https://discourse.processing.org/t/check-boolean-method-in-all-instances-for-true-false/18022/1 "2020-02-22T19:43:18Z")

</div>

This is not scalable, what is the correct way to go about checking to see if the boolean method in all instances are false, rather than && each instance?

```auto
for (Button b : buttons) {
    b.render();

    if (b.isHovering()) {
      cursor(HAND);
    } else if (!buttons[0].isHovering() && !buttons[1].isHovering() && !buttons[2].isHovering() && !buttons[3].isHovering()) { //there has got to be a shorthand way of doing this, i am just unaware of how
      cursor(ARROW);
    }
  }

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [February 22, 2020, 8:30pm UTC](https://discourse.processing.org/t/check-boolean-method-in-all-instances-for-true-false/18022/2 "2020-02-22T20:30:54Z")

</div>

Hello,

I am not sure what the best way is…

I wrote this to check for a _false_ in the array:

```auto
// Check for True False in an array
// v1.0.0
// GLV 2020-MM-DD

boolean [] test;
boolean state = false;
int count = 0;

void setup() 
	{
  size(200, 200);
  
  //create a test array that is all true
  test = new boolean [100];
  for (int i = 0; i<100; i++)
    {
    test[i] = true; 
    }
  // randomly assign a false to one element of test array 
  test[int(random(0, 100))] = false;  
	
  //check for false element in test and set state and return
  //for(boolean j: test)
  // {    
  // println(j, count);  
  // if (!j)
  // {
  // state = true;    
  // break; //exits when false
  // }
  // else
  // state = false; 
  // count++; 
  // }
  
  //Replaces above
  println(checkState(test), count);  
  }

void draw() 
	{
  background(0);
	}

//returns true if it finds a false
boolean checkState(boolean [] arr) 
  {
   for(boolean j:arr)
    {    
    println(j, count);  
    if (!j)
      {
      state = true;    
      break;
      }
    else
      state = false; 
    count++; 
    }
    
  return state;
  }

```

`:) `

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [February 22, 2020, 8:58pm UTC](https://discourse.processing.org/t/check-boolean-method-in-all-instances-for-true-false/18022/3 "2020-02-22T20:58:30Z")

</div>

Works for now, still doesn’t feel very SOLID, there has to be a way to iterate through each instance and check the methods return value.

```auto
for (int i = 0; i < buttons.length; i++) {
    hovers[i] = buttons[i].isHovering();
  }

 for (Button b : buttons) {
    b.render();
  }
  
  if (checkHoverState(hovers)) {
      cursor(HAND);
    } else {
      cursor(ARROW);
    }
}

boolean checkHoverState(boolean [] arr) {
  for (boolean a : arr) {
    if (a) {
      hoverState= true;
      break;
    } else {
      hoverState= false;
    }
  }
  return hoverState;
}

```

I appreciate your help … this feels better than where I started.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [February 22, 2020, 9:45pm UTC](https://discourse.processing.org/t/check-boolean-method-in-all-instances-for-true-false/18022/4 "2020-02-22T21:45:33Z")

</div>

I had a _break_ in there for a reason:  
[https://processing.org/reference/break.html](https://processing.org/reference/break.html)

If I am checking for all _true_ I _break_ when I find the first _false_ and vice versa.

Feel free to adapt for your own use.

`:)`

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 22, 2020, 10:02pm UTC](https://discourse.processing.org/t/check-boolean-method-in-all-instances-for-true-false/18022/5 "2020-02-22T22:02:33Z")

</div>

You have a for loop inside the for loop now

Bad

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [February 22, 2020, 10:07pm UTC](https://discourse.processing.org/t/check-boolean-method-in-all-instances-for-true-false/18022/7 "2020-02-22T22:07:28Z")

</div>

ahh yeah. makes sense. updated. ty

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [February 22, 2020, 10:09pm UTC](https://discourse.processing.org/t/check-boolean-method-in-all-instances-for-true-false/18022/8 "2020-02-22T22:09:50Z")

</div>

accident … fixed now. ty!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 22, 2020, 10:12pm UTC](https://discourse.processing.org/t/check-boolean-method-in-all-instances-for-true-false/18022/9 "2020-02-22T22:12:18Z")

</div>

looks still very bad…

> [@slow\_izzm](#):
>
> for (int i = 0; i \< buttons.length; i++) {  
> hovers[i] = buttons[i].isHovering();  
> }

not needed.

You got 3 for loops instead of 1.

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [February 22, 2020, 10:15pm UTC](https://discourse.processing.org/t/check-boolean-method-in-all-instances-for-true-false/18022/10 "2020-02-22T22:15:28Z")

</div>

how would you optimize it?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 22, 2020, 10:15pm UTC](https://discourse.processing.org/t/check-boolean-method-in-all-instances-for-true-false/18022/11 "2020-02-22T22:15:44Z")

</div>

> [@slow\_izzm](#):
>
> for (Button b : buttons) {  
> b.render();  
> }

That’s your good loop. Try to implement the other steps inside it

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [February 22, 2020, 10:16pm UTC](https://discourse.processing.org/t/check-boolean-method-in-all-instances-for-true-false/18022/12 "2020-02-22T22:16:32Z")

</div>

k … i see what your getting at

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 22, 2020, 10:20pm UTC](https://discourse.processing.org/t/check-boolean-method-in-all-instances-for-true-false/18022/13 "2020-02-22T22:20:51Z")

</div>

> [@slow\_izzm](#):
>
> hoverState= false;

say this before the for loop - that’s your default

Don’t use `break` obviously. (You want **all** buttons to render.)

Then

```auto

if (b.isHovering()) {
      hoverState= true;
} 

```

(without the else-part)

This means, when only ONE b has the mouse hovering, hoverState gets true and stays true.

Evaluate hoverState **after** the for loop

Chrisir

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [February 22, 2020, 11:01pm UTC](https://discourse.processing.org/t/check-boolean-method-in-all-instances-for-true-false/18022/14 "2020-02-22T23:01:24Z")

</div>

Sorry I was getting confused with the method glv proposed and yours …

this is perfect! ty!!

```auto
hoverState = false;
  for (Button b : buttons) {
    b.render();
    if (b.isHovering()) {
      hoverState = true;
    }
  }

  if (hoverState) {
    cursor(HAND);
  } else {
    cursor(ARROW);
  }

```
