Home > Back-end >  The method countTrue(boolean[]) in the type GUI is not applicable for the arguments (boolean, boolea
The method countTrue(boolean[]) in the type GUI is not applicable for the arguments (boolean, boolea

Time:10-02

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]);
            
    }

}

CodePudding user response:

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)

CodePudding user response:

Instead of boolean [] in your countTrue method, declare the parameter as boolean...

  • Related