Results 1 to 10 of 10
  1. #1
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default switch case with '<='

    Dumb question, but I keep getting an error.

    Error in Eclipse says "Invalid char constant" when I do the following:


    case'==':

    OR

    case '<=':

    The rest like case '<': are just fine. I have used " ", but those are for strings.

    How can I get the error to go away?
    Last edited by JosAH; 02-17-2013 at 03:59 PM.

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    392
    Rep Power
    5

    Default Re: switch case with '<='

    Hi Adomini,

    Anything enclosed in single quotes is seen as a single char which is why '<' works ok. You cannot use '==' as this is an invalid use of the single quotes.

    Regards.

  3. #3
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default Re: switch case with '<='

    Right. But "" does not work either because those are set for Strings.

    when I do two of the singular marks ( '' ) it does not work either. There has got to be a way to do this case-wise without resorting to if-else statements.

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    392
    Rep Power
    5

    Default Re: switch case with '<='

    Strings are permitted within switch statements in Java 7.
    Can you show us exactly what you are attemting to code.

    Regards.

  5. #5
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default Re: switch case with '<='

    Sure!

    switch (line.charAt(index))
    {
    case '+':
    tok = TokenType.ADD_OP;
    break;
    case '<=':
    tok = TokenType.LESSOREQUAL;
    break;

    ** Ignore the tokenTypes - these are fine. Its just the case '<=' and such like it that cause the error.

  6. #6
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    392
    Rep Power
    5

    Default Re: switch case with '<='

    The switch is not going to work in the way you intend. The parameter used in your switch statement is a single char so even if this was a valid function '<=' will never match as this are two chars.

    You will need to rethink your switch parameter or case statements.

    Regards.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default Re: switch case with '<='

    Why not use an enum type? Then you wouldn't need a switch statement. Here is a sample of what you might do.

    Java Code:
    enum TokenType {
       ADDOP("+"), LESSOREQUAL("<="), GREATEROREQUAL(">=");
       private String token;
       private static Map<String, TokenType> tokens = new HashMap<String, TokenType>();
       private TokenType(String token) {
          this.token = token;
       }
       static {
          for (TokenType t : values() ) {
             tokens.put(t.getToken(),t);
          }
       }
       
       public String getToken() {
          return token;
       }
       
       public static TokenType getValue(String token) {
          return tokens.get(token);
       }
       
    }
    public class enumTest {
       public static void main(String[] args) {
          
          String[] tokens = new String[]{ "+", "<=", ">="
          };
          
          for (TokenType t: TokenType.values()) {
             System.out.println(t.name() + "  " + t.getToken());
          }
          
          for (String t : tokens) {
             System.out.println(t + " maps to " + TokenType.getValue(t));
          }
    
       }
    
    }
    Enums are great for this type of thing. You can even use them as an entire class for a pretty indestructible singleton.

    Regards,
    Jim

  8. #8
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default Re: switch case with '<='

    Jim,

    I feel really bad if you just typed up that whole thing. I used some if -else logic in a couple of my "cases" so it flowed.

    I looked on how to put this thread as solved.... but could not find it. Dude, I am so sorry.....

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default Re: switch case with '<='

    Absolutely no reason to apologize. I enjoy this stuff. Perhaps someone can use the construct.

    Regards,
    Jim

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default Re: switch case with '<='

    Quote Originally Posted by Adomini View Post
    I looked on how to put this thread as solved.... but could not find it. Dude, I am so sorry.....
    I did it for you; you have to edit your OP in 'advanced mode' and add a prefix to your title.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. error when i use switch-case
    By amrmb09 in forum Advanced Java
    Replies: 1
    Last Post: 12-09-2011, 12:29 AM
  2. about conditonal if else and switch case
    By niksipandit in forum New To Java
    Replies: 6
    Last Post: 09-22-2011, 11:53 AM
  3. if else changes to switch-case?
    By noobinoo in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 05:56 PM
  4. Replies: 2
    Last Post: 02-23-2009, 12:34 AM
  5. Switch Case and Key Events
    By AndrewM16921 in forum New To Java
    Replies: 4
    Last Post: 01-26-2009, 11:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •