switch with a string

Ola,

I want to do something like a switch but then for usage with strings.
as I understand switch is used switch ( int )

But how do i emulate switch ( String )

I am thinking of something like this
switch ( FunctionThatTransformsStringToInt( SomeStringVariable ) )
{
  case FunctionThatTransformsStringToInt( "SomeSting" ) :
}

how to make something like this work or what other ways are there?
LVL 2
DaFouAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:
the problem is "case" token only takes constant expersion, but u can do something like that:

public class SwitchString {
   
    public static void main(String[] args) {
        String color = "white";
       
        switch(color.hashCode())
        {
         case 12:
             System.out.println("Yellow!!");
         
        }
    }
}

assuming u know the hashCode values already
0
petmagdyCommented:
to clarify more like this:

public class SwitchString {
   
    public static void main(String[] args) {
        String color = "white";
        int yellowCode = "yellow".hashCode();
        int whiteCode = "white".hashCode();
         System.out.println(yellowCode);
         System.out.println(whiteCode);
        switch(color.hashCode())
        {
         case -734239628:
             System.out.println("Yellow!!");
         case 113101865:
             System.out.println("White!!");
         
        }
    }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
petmagdyCommented:
also what is wrong with?:

if("while".equals(color) )
{

}
else if("yellow".equals(color) )
{

}
.....
0
DaFouAuthor Commented:
with more then 2 options anything other then a switch will result in performance loss
also using a switch statment one can group situation like

switch ( int )
{
  case 1 :
  case 2 :
    // do something
    break;
  case 3 :
  // do something else
}

and the switch simply looks much better then else if
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.