2

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;
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
maker1
  • 21
  • 1
  • 4
  • 5
    Because it is? Do you know what the words mean? Did you try looking them up, say, in a dictionary? – Karl Knechtel Nov 04 '12 at 02:35
  • Thank you captain obvious. Appreciate your help but being rude does not resolve my issue. – maker1 Nov 04 '12 at 02:39
  • 1
    It's called a character constant because it consists of one character. – Karl Knechtel Nov 04 '12 at 02:39
  • 1
    I'm with Karl on this one. This is one of those common sense things, like when the compiler throws up an error about a missing semicolon when you're missing a semicolon. "What is wrong with thise code???? Stupid compiler!!!!" – ta.speot.is Nov 04 '12 at 02:40
  • Thanks. Lets take into consideration what is common sense to you may not be to me. This is only my 2nd java programming class so please do not attempt to downplay my experience or intellect. I have resolved my own issue. Nothing further. – maker1 Nov 04 '12 at 02:42

2 Answers2

8

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.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Right. Tried the String value. Did not work. Also found you cannot switch on them without using java 7+. Thank you. I will find another way to resolve – maker1 Nov 04 '12 at 02:38
  • @user1797439 `String`s are objects so you have to use `s1.equals(s2)`, not `==`. – Kevin Nov 04 '12 at 02:58
  • what String are you referring to? I know how to compare strings. But I see no strings in my code being compared. – maker1 Nov 04 '12 at 03:04
3

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.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523