mardi 2 octobre 2018

Check if an character is an operator

I want to check if a character is an operator like : %,/,*,+,-

This is the code to get input from the user in the main function:

 Scanner input = new Scanner(System.in);

        System.out.println("Operator (S is stoppen)");

        String operator = input.nextLine();
        char o = operator.charAt(0);

So the input is stored in the variabele 'o'

So now I tried to make a new function to check if the character is one of these functions: %,/,*,+,-

this is the function I tried to make:

static boolean isGeldigeOperator(char o) {

    if (o == '%' || '/' || '*' || '+' || '-'){
        return true;
    } else{
        return false;
    }

So if o == one of the operators return true and if not return false.

Now the error that I'm getting is about this line:

 if (o == '%' || '/' || '*' || '+' || '-'){

Its this error: Operator || cannot be applied to 'boolean', 'char'

Does anyone know what I'm doing wrong?

Aucun commentaire:

Enregistrer un commentaire