Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

you cant switch a String?  RSS feed

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why can't I switch a java.lang.String object?

says i have to pass an integer?

anyway around this?

Justin
[ September 18, 2006: Message edited by: Bear Bibeault ]
 
Jean-Francois Briere
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use if else if instead.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or maybe look into using enums instead of Strings.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, i used if else, but i have to look at input and decide if its a "special command" or not, then execute that special command.

Example:

#logoff
#sethost <hostname>
#setport <port>
etc...

i am extracting the input, and using substring to get the names and
seeing if its a command..

any easier way?

Justin
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,

You could put the key words in a dictionary (Hashmap in Java) and if it is a command, do command something - assuming that one command something is similar to another command something.

If every command is different (say like a compiler checking the input) and if you do not have a formal language to define it, then a series of if .. elses is probably the best way.

If the commands have a lot in common, put a dictionary with key:value where key is the command and value is an int value you can use for the switch.

-steve
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Fahlbusch:
Well,

You could put the key words in a dictionary (Hashmap in Java) and if it is a command, do command something - assuming that one command something is similar to another command something.

If every command is different (say like a compiler checking the input) and if you do not have a formal language to define it, then a series of if .. elses is probably the best way.

If the commands have a lot in common, put a dictionary with key:value where key is the command and value is an int value you can use for the switch.

-steve


Or better yet, take that one step further and use Strategy instead of a switch. If you're going to be looking at the command as a String simply use that String as a key to map to a Command object or something similar. Then your code ends up looking something like this:



Certainly preferable to a giant switch statement in my eyes. Has the benefit of being able to map a String to any Command, even changing it at runtime based upon a configuration file or Preferences.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i will definately read up on this "strategy", but i got it working with
if\else, and I am too lazy to change it now lol...

thanks again,

Justin
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is how the switch is suppose to work.These are java language constructs and if our program doesnot adhere to this then it would raise a
compile time error.

It is something like we cannot have a statement like

new String() = a String;

we need to write it as
String a = new String();

These are the java language constructs.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!