Optimizing if-else /switch-case with string options

Posted by cc on Stack Overflow See other posts from Stack Overflow or by cc
Published on 2009-10-08T06:32:38Z Indexed on 2010/03/28 14:23 UTC
Read the original article Hit count: 426

Filed under:
|

What modification would bring to this piece of code? In the last lines, should I use more if-else structures, instead of "if-if-if"

if (action.equals("opt1"))
{
    //something
}
else
{
    if (action.equals("opt2"))
    {
        //something
    }
    else
    {
        if ((action.equals("opt3")) || (action.equals("opt4")))
        {
            //something
        }
        if (action.equals("opt5"))
        {
            //something
        }
        if (action.equals("opt6"))
        {
            //something
        }
    }
}

Later Edit: This is Java. I don't think that switch-case structure will work with Strings.

Later Edit 2:

A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Classes and Inheritance) and a few special classes that "wrap" certain primitive types: Character, Byte, Short, and Integer (discussed in Simple Data Objects ).

© Stack Overflow or respective owner

Related posts about beginner

Related posts about java