File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Can switch() be given more capability ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can switch() be given more capability ?" Watch "Can switch() be given more capability ?" New topic
Author

Can switch() be given more capability ?

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

It can only handle ints, chars...and enums. Why cant they make it take strings as well ?


SCJP 6. Learning more now.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

Java 7 has an update for that- Project Coin

[Edit: Added the Links]


Mohamed Sanaulla | My Blog
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

Rahul Sudip Bose wrote:It can only handle ints, chars...and enums. Why cant they make it take strings as well ?


I believe this is an enhancement for Java 7.

[EDIT: beaten to the answer again]

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

I just started making my own class PowerSwitch{} for fun- please tell me if i am thinking in the right direction. (its just in the planning phase)
this will take strings, convert them to lowercase and use them as keys in a map. these keys will be mapped to int objects which will be used in the switch of this class.

Any suggestions , ideas etc welcome.

PS : i was wondering if i should leave this since others are already doing it and...well there is scjp
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Henry Wong wrote:
Rahul Sudip Bose wrote:
[EDIT: beaten to the answer again]


i am not so sure what this means.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

You can always map the int with some string values, use the int for switch and then retrieve the string based on the int value.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

Rahul Sudip Bose wrote:
Henry Wong wrote:
[EDIT: beaten to the answer again]


i am not so sure what this means.


that means- there a difference of less than a minute between my and Henry's answer.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Rahul Sudip Bose wrote:It can only handle ints, chars...and enums. Why cant they make it take strings as well ?

. . . or anything which can be implicitly cast to an int, ie shorts and bytes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can switch() be given more capability ?
 
Similar Threads
WA #1.....word association
Can the color for the disabled item be changed
Private methods
URGENT - NEED HELP - Block Code
error in Marcus Green 2.