Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if an array has a certain character, why is my code not working?

Hello here is some code to check for a valid character, although the code is not working. Even if 'operand' is a valid character it will never print yes, and return false. No matter what even if the character is valid it just doesn't recognize it and always goes to the else statement. Please help!

public static boolean checkValidOperands(char operand) {

    char[] validOperators = {'+', '-', '*', '/', 'q'};
    List<char[]> validOp = Arrays.asList(validOperators);
    if (validOp.contains(operand))  {
        System.out.println("Yes");
        return false;
    }  else {
        System.out.println("Please enter valid operand");
        return true;
    }
}
like image 774
Nearpoint Avatar asked Jan 14 '23 02:01

Nearpoint


1 Answers

You could use:

List<Character> validOp = Arrays.asList('+', '-', '*', '/', 'q');
like image 59
Reimeus Avatar answered Jan 19 '23 10:01

Reimeus