Question

Hello I am getting an error on '<=' that it is an invalid character constant? Any ideas why?

switch (ch)
    {   
        case '+' : res = op1+op2;break;   
        case '-' : res = op1-op2;break;  
        case '*' : res = op1*op2;break;
        case '/' : if (op2 != 0)
                    res = op1/op2;
                   else 
                   System.out.println("Division by zero error in"+
                   " PostfixEvaluator.calculate().");
                   break;  
        case '%' : if (op2 != 0)
                    res = op1%op2;
                   else 
                   System.out.println("Division by zero error in"+
                   " PostfixEvaluator.calculate().");
                   break;  
        /**
         * Alterations begin here
         */
        case '<' : if(op1 < op2)
                    res = 1;
                   else
                    res = 0;
                   break;
        case '<=' : if(op1 <= op2)
                     res = 1;
                   else
                     res = 2;
                   break;
        case '>' : if(op1 > op2)
                     res = 1;
                   else
                     res = 2;
                   break;
        case '>=' : if(op1 >= op2)
                     res = 1;
                    else
                     res = 2;
                    break;
        case '==' : if(op1 == op2)
                     res = 1;
                    else
                     res = 2;
                    break;
        case '!=' : if(op1 != op2)
                     res = 1;
                    else
                     res = 2;
                    break;

        case '||' : if(true || false )
                      res = 1;
                    else if(false || true)
                      res = 1;
                    else if(false || false)
                      res = 0;
                    else 
                      res = 1;
                    break;
        case '&&' : if(true && false )
                      res = 0;
                    else if(false && true)
                      res = 0;
                    else if(false && false)
                      res = 0;
                    else
                      res = 1;
                    break;

No correct solution

OTHER TIPS

A character in Java is a single letter (so to speak), so '<=', '&&', etc. won't work. If you need to put more than one character, then you have to use a String: "<=" noticing the double quotes.

But then again, in Java you can't switch on a String value unless you're using Java 7+. If that is not the case, then you'll have to use plain old if-else if statements for performing the comparisons.

Character constants must denote exactly one character*: you cannot use && or || as a character constant, because they have two characters.

If you go character-by-character, common solution is to use a single |, and then check the prior character to see if it's also | or &.


* Escaped sequences such as \n also denote a single character, even though they consist of two characters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top