Advertisement

08.04.2004 at 01:12AM PDT, ID: 21081620
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

switch statement on a java string
Tags: java, switch, string
Hi,
is there away to do java switch statment on a string variable? I know it can be done on integer but not strings. any work around if I need to do a switch case statements on a string variable in java?

Thanks
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: saedpalnet
Solution Provided By: zzynx
Participating Experts: 9
Solution Grade: A
Views: 669
Translate:
Loading Advertisement...
08.04.2004 at 01:15AM PDT, ID: 11713106

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 01:18AM PDT, ID: 11713119

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 01:20AM PDT, ID: 11713132

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 01:21AM PDT, ID: 11713136

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 01:21AM PDT, ID: 11713137

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 01:27AM PDT, ID: 11713176

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 01:29AM PDT, ID: 11713184

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 01:33AM PDT, ID: 11713200

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 01:36AM PDT, ID: 11713221

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 01:37AM PDT, ID: 11713225

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 01:41AM PDT, ID: 11713247

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 01:45AM PDT, ID: 11713265

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 01:48AM PDT, ID: 11713284

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 01:51AM PDT, ID: 11713300

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 01:52AM PDT, ID: 11713306

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 01:58AM PDT, ID: 11713323

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 02:10AM PDT, ID: 11713377

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 02:12AM PDT, ID: 11713387

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 02:13AM PDT, ID: 11713400

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 02:13AM PDT, ID: 11713401

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 02:16AM PDT, ID: 11713417

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 02:16AM PDT, ID: 11713419

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 02:19AM PDT, ID: 11713437

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 02:24AM PDT, ID: 11713461

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 02:28AM PDT, ID: 11713488

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 02:37AM PDT, ID: 11713544

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 07:05AM PDT, ID: 11715674

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.04.2004 at 05:52PM PDT, ID: 11722354

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.05.2004 at 12:04AM PDT, ID: 11723717

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.05.2004 at 12:14AM PDT, ID: 11723778

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.05.2004 at 01:16AM PDT, ID: 11724044

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.21.2005 at 12:30PM PST, ID: 13106517

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
08.04.2004 at 01:15AM PDT, ID: 11713106
Nope.
 
08.04.2004 at 01:18AM PDT, ID: 11713119

Rank: Guru

you can use switch on int char and other primitive type.
String is not a primitive type, but Object type.

Bye, Giant.
 
08.04.2004 at 01:20AM PDT, ID: 11713132
Hehe, the workaround:

if(str.equals("A")) {
  ..
}
else if(str.equals("B")) {
  ..
}

The funny workaround (don't use this one):

switch(str.hashCode())
{
  case 0x01234567:
    ..
    break;
  case 0x89abcdef:
    ..
    break;
}

It will give you a switch in the code, but it won't work reliable.
 
08.04.2004 at 01:21AM PDT, ID: 11713136

Rank: Guru

In particular switch could be used with:
char, byte, short, int.
 
08.04.2004 at 01:21AM PDT, ID: 11713137

Rank: Sage

If it is a one char string you coudl convert it to char and do it, otherwise there is no way as staed above.
 
08.04.2004 at 01:27AM PDT, ID: 11713176
I cant believe java is weak at this... one can easily do this in other languages like PHP for example!!
its hard to convince my manager who knows something about programming that one cant do a switch case on a string in java !!:)
I tried the .hashCode solution long time ago but thought it looks ugly with bad results too!
 
08.04.2004 at 01:29AM PDT, ID: 11713184

Rank: Guru

you can use a "simulated" switch statement:
if
else if
else if
else if
...

but I believe it's not really elegant.
Assisted Solution
 
08.04.2004 at 01:33AM PDT, ID: 11713200

Rank: Genius

>> is there away to do java switch statment on a string variable?
>> I know it can be done on integer but not strings.
Nice to answer your Q yourself. ;°)

>> any work around
First, what's wrong with the if/else if pair?

But if you really want one:

If you don't have too much strings you know beforehand,
you could make a unique link between the string and an integer by using a list:

List theList = new ArrayList();
theList.add("StringAtIndex0");
theList.add("StringAtIndex1");
theList.add("StringAtIndex2");
theList.add("StringAtIndex3");
...

String theString = "StringAtIndex2";
int index = theList.indexOf(theString);
switch(index) {
   case 0: // means StringAtIndex0

      break;

   case 1: // means StringAtIndex1

      break;

   case 2: // means StringAtIndex2

      break;

   case 3: // means StringAtIndex3

      break;

   default :
      break;
}
Accepted Solution
 
08.04.2004 at 01:36AM PDT, ID: 11713221

Rank: Master

Map strings to constant varibales of int type with names similar as Strings  and use reflection to get them.

i.e. If you want to switch for
String s = "HELLO";
String s1 = "HELLO2";

create an integer like

const int HELLO=1;
const int HELLO2=2;

and so on

Then use the reflection to get the names of the variables according to the strings and use switch on them

:)
 
08.04.2004 at 01:37AM PDT, ID: 11713225

Rank: Genius

>> its hard to convince my manager who knows something about programming that one cant do a switch case on a string in java !!:)
Be glad you have one *who knows something about programming*. Most of them don't. ;°)
 
08.04.2004 at 01:41AM PDT, ID: 11713247
If you need to convince your manager:

Suppose a switch with an Object is possible would the case statements be equal when the reference is equal or when the content is equal?
If the reference should be equal the switch is useless. If the content should be equal which method should be used for that evaluation?
Either by implying the use of equals() (loosened semantics) or by allowing operator overloading.
PHP has loosened semantics, which leads to a kind of fuzzy logic in the code (the string is equal enough for me).
C++ has operator overloading, which leads to unreadable code (maybe == is properly overloaded, maybe not).

I like the Java solution, clear semantics, no operator overloading, can't switch on an Object.
Assisted Solution
 
08.04.2004 at 01:45AM PDT, ID: 11713265

Rank: Genius

And a switch is just a glorified if/else anyways
 
08.04.2004 at 01:48AM PDT, ID: 11713284

Rank: Genius

>> Then use the reflection
Think then you can better use a map to make the link between the Strings and the ints:

private final String STR_HELLO = "Hello";
private final String STR_WORLD = "World";
...

private final int INT_HELLO=1;
private final int INT_WORLD=2;
...

Map theMap = new HashMap();
theMap.put( STR_HELLO, new Integer(INT_HELLO) );
theMap.put( STR_WORLD, new Integer(INT_WORLD) );
...

String theString = .....;
int value = theMap.contains(theString) ? ((Integer)theMap.get(theString)).intValue() : -1;
switch (value) {
   case INT_HELLO:
       break;
   case INT_WORLD:
       break;
   default:
       break;
}
 
08.04.2004 at 01:51AM PDT, ID: 11713300

Rank: Guru

>And a switch is just a glorified if/else anyways
Full agree, as I posted before.
 
08.04.2004 at 01:52AM PDT, ID: 11713306

Rank: Genius

> Think then you can better use a map to make the link between the Strings and the ints:

though far simpler to just use an if/then/else
 
08.04.2004 at 01:58AM PDT, ID: 11713323

Rank: Genius

> its hard to convince my manager who knows something about programming that one cant do a switch case on a string in java

ask him under what situation you would need a switch, that could not be handled by an if/then/else statement
 
08.04.2004 at 02:10AM PDT, ID: 11713377

Rank: Guru

:)
 
08.04.2004 at 02:12AM PDT, ID: 11713387
ok guys...
thanks alot for you comments... I think I got the answer I want. I wanted mainly to be sure and find a more elegant way of doing the coding. I have to split the points, which is not much anyways, I hope I did it right ... thanks to you all  armoghan, girionis, wolfc, Giant2 and zzynx :)
Regards
 
08.04.2004 at 02:13AM PDT, ID: 11713400

Rank: Guru

You could do something on these lines, but you'd have to watch for duplicate strings.


  String st = "Hello";

  byte[] b = st.getBytes();
  int[] intt = new int[b.length];
  int sum=0;

  for(int a=0;a<b.length;a++){sum+=(int)b[a];}

  System.out.println(sum);

   switch (sum){

     case (100):{}

  }
 
08.04.2004 at 02:13AM PDT, ID: 11713401
and objects for sure :)
 
08.04.2004 at 02:16AM PDT, ID: 11713417
thank you krakatoa, but I think I will just stick with the original if else solution!
 
08.04.2004 at 02:16AM PDT, ID: 11713419

Rank: Genius

> and objects for sure :)

;)
 
</