This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch error: incompatible type found

 
Zahir Sercan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you are using Java 7, you cannot use Switch with Strings. You should be using compile time constants to compare in the case.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might do better to split on "\\s+" unless you are sure you always have single spalces.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can switch on an enum, and an enum constant can hold a String value.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic