aspose file tools
The moose likes Beginning Java and the fly likes you cant switch a  String? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "you cant switch a  String?" Watch "you cant switch a  String?" New topic
Author

you cant switch a String?

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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 ]

You down with OOP? Yeah you know me!
Jean-Francois Briere
Ranch Hand

Joined: Mar 03, 2004
Posts: 101
Use if else if instead.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Or maybe look into using enums instead of Strings.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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
Ranch Hand

Joined: Sep 18, 2000
Posts: 496
    
    2

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

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Jan 24, 2006
Posts: 802
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

Joined: Nov 29, 2005
Posts: 2300
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.


Rahul Bhattacharjee
LinkedIn - Blog
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: you cant switch a String?
 
Similar Threads
QN about connetion pool usage in JSP
Tech Word Game
Switch gateway servers for connection
WA #2 ..... word association
Create HttpResponse 's object