| Author |
Switch error: incompatible type found
|
Zahir Sercan
Greenhorn
Joined: Jan 19, 2012
Posts: 1
|
|
Hi Java buddies,
Im building a expression tree calculator. Im now stuck with a problem here is the code snippet.
Method Postfix
Methode Solve
Any idea or advice what i can do to solve this problem?
Cheers
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2929
|
|
|
Unless you are using Java 7, you cannot use Switch with Strings. You should be using compile time constants to compare in the case.
|
Mohamed Sanaulla | My Blog
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
You might do better to split on "\\s+" unless you are sure you always have single spalces.
|
 |
dennis deems
Ranch Hand
Joined: Mar 12, 2011
Posts: 808
|
|
|
You can switch on an enum, and an enum constant can hold a String value.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5901
|
|
Mohamed Sanaulla wrote:Unless you are using Java 7, you cannot use Switch with Strings. You should be using compile time constants to compare in the case.
Doesn't the String switching require compile-time constants as well? That is:
Or am I just making baseless assumptions?
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2929
|
|
Jeff Verdegan wrote:
Mohamed Sanaulla wrote:Unless you are using Java 7, you cannot use Switch with Strings. You should be using compile time constants to compare in the case.
Doesn't the String switching require compile-time constants as well? That is:
Or am I just making baseless assumptions?
I dont know if any one approach is recommended. But both of them work though.
|
 |
 |
|
|
subject: Switch error: incompatible type found
|
|
|