How do I fix error "The operator && is undefined for the argument types boolean[], boolean"

Hi all,

I am making a trivia game for a class project and I am getting this error I have not seen before and cannot figure out how to fix it. Context is I am using XML to load information into the game and overMulti is a boolean[] and then overStart and overCont are booleans. It is the first overMulti if statement causing the problems but it also says the function checkAnswer doesn’t exist?? But that is a future problem, I think. Is this a situation where I have a few small errors and I can tinker and fix it or is my approach wrong? I am new to coding and cannot yet tell when I am a semicolon away from being right or like 20 lines of code away. Anyways, I would really appreciate any help or advice you guys have!

void mouseClicked() {
  if (overStart && state == 0) { //if mouse is clicked on start button AND the state is the start screen, 0, move to the game state, 1 (switch statement in draw with states)
    state = 1; 
    if (overCont && state == 2) { //if mouse is clicked on continue button AND the state is feedback screen, 2, move to the game state, 1
      state = 1;
      if (activeQuestion < questions.size()) {
        activeQuestion++;
      }
      if (overMulti && state == 1) { //if mouse is clicked on multiple choice button AND the state is game, 1, move to feedback screen, 2
        answers.get(activeQuestion).checkAnswer(); //check answer to activeQuestion
        state = 2;
      } else if (overCont && state == 2) { //else if mouse is clicked on continue button and the state is the feedback screen, 2, move to game, 1
        if (activeQuestion < questions.size()) { //move to next question in array
          activeQuestion++;
          state = 1;

          //check the multiple choice buttons for clicks
          for (int i = 0; i < overMulti.length; i++) {
            if (overMulti[i]) {
              questions.get(activeQuestion).guess = answers.get(activeQuestion).possAns[i];
              answered = true;
              //THIS IS WHERE THE USER ANSWERS CHECKED
            }
          }
        }
      }
    }
  }
}
1 Like

overMulti is an array?

1 Like

Either:

  1. overMulti should be declared as a boolean, not boolean[], or
  2. if it is an array of many booleans, you need to decide how to test it.

Is overMulti true

  1. if any of its booleans are true?
  2. if all of its booleans are true?

For any, you can define a function anyTrue

boolean anyTrue(boolean[] array) {
    for(boolean b : array) if(b) return true;
    return false;
}

(see https://stackoverflow.com/a/8260897/7207622)

and then you can use it in your test statement:

if (anyTrue(overMulti) && state == 1) {
1 Like

Well, while I’m definitely missing some information from this picture (namely I don’t know the datatype of every variable here), this error indicates that somewhere in this code, you are performing the boolean operation && between one boolean and one array of booleans. It’s worth noting that just because you got this error only once, does not mean it only happened once. Remember, you can access individual members of this class through [doing this with the index inside]. It appears overMulti is an array of booleans, which means the line

if(overMulti && state == 1) {

is invalid. Try instead,

if(overMulti[index] && state ==1) {

but replace index with whatever index you’d like to access. Or, if you want to make sure they’re all true, or at least one is true, try creating a function that accepts boolean arrays as inputs, and returns if all/at least one is valid. It really depends on what you’re trying to accomplish, but an array cannot attain the specific value of true or false, only the elements of this array can have specific values. Hope this helps! :slight_smile: