Skip to content
Advertisement

The method countTrue(boolean[]) in the type GUI is not applicable for the arguments (boolean, boolean, boolean, boolean, boolean)

this is the error :

“The method countTrue(boolean[]) in the type GUI is not applicable for the arguments (boolean, boolean, boolean, boolean, boolean)” , the error is in the last line , but i dont get why.

public class GUI {
    
    public GUI(){
        
    }
    
    public static int countTrue(boolean[] arr) {
        int count = 0;
        for (boolean element : arr) {
            if (element == true ) {
                count++;
            }
        }
        
        return count ;
    }
    
    
    public static void main(String[] args) {
        
        int x = GUI.countTrue([true, false, false, true, false]);
            
    }

}

Advertisement

Answer

New array should be declared as below:

int x = GUI.countTrue(new boolean[]{true, false, false, true, false});

OR

you can use varargs and declare your method as: public static int countTrue(boolean... arr)

and then call it as: int x = GUI.countTrue(true, false, false, true, false);

Also you can simplify if (element == true) to just if (element)

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement