Results 1 to 10 of 10
Thread: switch case with '<='
- 02-16-2013, 11:34 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
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.
- 02-16-2013, 11:42 PM #2
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
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.
- 02-16-2013, 11:53 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
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.
- 02-16-2013, 11:58 PM #4
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
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.
- 02-17-2013, 12:04 AM #5
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
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.
- 02-17-2013, 12:17 AM #6
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
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.
- 02-17-2013, 01:16 AM #7
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 697
- Rep Power
- 1
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.
Enums are great for this type of thing. You can even use them as an entire class for a pretty indestructible singleton.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)); } } }
Regards,
Jim
- 02-17-2013, 02:25 AM #8
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
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.....
- 02-17-2013, 02:41 AM #9
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 697
- Rep Power
- 1
Re: switch case with '<='
Absolutely no reason to apologize. I enjoy this stuff. Perhaps someone can use the construct.
Regards,
Jim
- 02-17-2013, 04:01 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
error when i use switch-case
By amrmb09 in forum Advanced JavaReplies: 1Last Post: 12-09-2011, 12:29 AM -
about conditonal if else and switch case
By niksipandit in forum New To JavaReplies: 6Last Post: 09-22-2011, 11:53 AM -
if else changes to switch-case?
By noobinoo in forum New To JavaReplies: 1Last Post: 04-23-2010, 05:56 PM -
[SOLVED] Is it possible to have If in a switch case?
By sfe23 in forum New To JavaReplies: 2Last Post: 02-23-2009, 12:34 AM -
Switch Case and Key Events
By AndrewM16921 in forum New To JavaReplies: 4Last Post: 01-26-2009, 11:20 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks