4

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;
    }
}
5
  • That's a List of char[]'s, not a List of char's. Commented Mar 1, 2013 at 2:49
  • how can I fix this code so it works? Commented Mar 1, 2013 at 2:50
  • See this question: stackoverflow.com/questions/1248763/arrays-aslist-of-an-array Edit: Or @Reimeus answer Commented Mar 1, 2013 at 2:50
  • Are you sure the declaration for validOp shouldn't be a List<Char> rather than a List<Char[]>? You're not looking for a list of character arrays, but just characters... Commented Mar 1, 2013 at 2:51
  • Yup looks like this is the problem, I tried List<Char> but <Char> is not recognized... Commented Mar 1, 2013 at 2:54

3 Answers 3

7

You could use:

List<Character> validOp = Arrays.asList('+', '-', '*', '/', 'q');
Sign up to request clarification or add additional context in comments.

Comments

2

The way you are creating a list of characters is wrong.

In your current code the list that you made is actually a list of character arrays, instead of 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 for future, do not use List<char>, List<int> etc.. as you cannot use a primitive type for generic in Java. Use their corresponding Object counter parts instead. Refer to this question for more information Why can Java Collections not directly store Primitives types?

Comments

0

A string is a better data structure for a set of characters. For example,

String validOperators = "+-*/q";

if (validOperators.indexOf(operand) != -1)  {
    System.out.println("Yes");
    return false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.