The way you create a list of characters is wrong.
In your current code, the list you created is actually a list of arrays of characters, not characters.
import java.util.Arrays; import java.util.List; public class Test{ public static void main(String[] args){ char ch = '+'; System.out.println(checkValidOperands(ch)); } public static boolean checkValidOperands(char operand) { Character[] validOperators = {'+', '-', '*', '/', 'q'}; List<Character> validOp = Arrays.asList(validOperators); if (validOp.contains(operand)) { System.out.println("Yes"); return false; } else { System.out.println("Please enter valid operand"); return true; } } }
PS: Also in the future, do not use List<char> , List<int> , etc., since you cannot use a primitive type for the general in Java. Instead, use the corresponding counter elements. See this question for more information. Why can't Java collections directly store primitive types?
source share