aspose file tools*
The moose likes Java in General and the fly likes Switch error: incompatible type found Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Switch error: incompatible type found" Watch "Switch error: incompatible type found" New topic
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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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: 38363
    
  23
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: 6109
    
    6

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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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.
 
wood burning stoves
 
subject: Switch error: incompatible type found