0

I have a boolean Array. I want to check if all of this booleans are true.

How can I print a message, if all of them are true?

public static boolean[] tubes = new boolean[9];
slartidan
  • 20,403
  • 15
  • 83
  • 131
  • What have you tried? There are several ways this could be implemented. – Dave Newton Mar 19 '21 at 20:19
  • I tried it with an for loop. An then checked it with if(Tubes[i] == true) System.out.println("All of them are true"); But this check every boolean and outputs that 9 times. I only want 1 output which says that all of them are true. I hope you understand sry for my english. – TechnikFabrik Mar 19 '21 at 20:23
  • i already checked this. but no solution for my problem :/ – TechnikFabrik Mar 19 '21 at 20:24
  • @TechnikFabrik The for loop should store the *aggregation* of values (and can break as soon as an unmatching value is found). – Dave Newton Mar 19 '21 at 20:34

2 Answers2

1

You can loop elements and store its result in a variable as shown below:-

boolean isTrue = false;
for (boolean bool : tubes) {
    isTrue = bool;
    if (!isTrue) {
       break;
    }
}

if (isTrue) {
  // print...
}

Another solution

boolean isTrue = true;
for (boolean bool : tubes) {
    isTrue &= bool;
}

if (isTrue) {
  // print...
}
Montaser Sobaih
  • 305
  • 2
  • 7
0

Do you mean like this?

    public boolean checkFunction(boolean[] Tubes) {
    //boolean[] Tubes = new boolean[9];
    boolean et = true; // by default, every member of the array is true

    for(int i=0; i < 9;i++) {
        if (Tubes[i] == false) { // if i find a false item then its false
            System.out.println("This is false: " + Tubes[i]);
            et = false;
        }
    }
    return et;
}