Nidhi
Nidhi

Reputation: 276

Converting IF-else construct to SWITCH in eclipse

I understand that we can convert if-else construct to switch in java, using Quick Assist (CTRL+1 ) in eclipse. However, I have a situation here, where I have to make workspace wide change. Is anyone aware how that will be done.

Also, any idea how the below construct can be converted using a tool?

if (getBcListEntryArr38(wsCount).getBcRenewal0cd() == '0') {
    getBcListEntryArr38(wsCount).setBcRenewal0cdDesc("Alright For Agent To Renew");
} else {
    if (getBcListEntryArr38(wsCount).getBcRenewal0cd() == '1') {
        getBcListEntryArr38(wsCount).setBcRenewal0cdDesc("Computer Renew");
    } else {
        if (getBcListEntryArr38(wsCount).getBcRenewal0cd() == '2') {
            getBcListEntryArr38(wsCount).setBcRenewal0cdDesc("Alright For Home Office To renew");
        } else {
            if (getBcListEntryArr38(wsCount).getBcRenewal0cd() == '3') {
                getBcListEntryArr38(wsCount).setBcRenewal0cdDesc("Already Renewed");
            } else {
                if (getBcListEntryArr38(wsCount).getBcRenewal0cd() == '4') {
                    getBcListEntryArr38(wsCount).setBcRenewal0cdDesc("Agent Issued First Policy, Computer To Renew");
                } else {
                    if (getBcListEntryArr38(wsCount).getBcRenewal0cd() == '7') {
                        getBcListEntryArr38(wsCount).setBcRenewal0cdDesc("Non-Renewal With A Notice");
                    } else {
                        if (getBcListEntryArr38(wsCount).getBcRenewal0cd() == '8') {
                            getBcListEntryArr38(wsCount).setBcRenewal0cdDesc("Non-Renewal Without A Notice");
                        } else {
                            if (getBcListEntryArr38(wsCount).getBcRenewal0cd() == '9') {
                                getBcListEntryArr38(wsCount).setBcRenewal0cdDesc("Cancelled Policy");
                            } else {
                                getBcListEntryArr38(wsCount).setBcRenewal0cdDesc("");
                            }
                        }
                    }
                }
            }
        }
    }

Upvotes: 5

Views: 1982

Answers (3)

VonC
VonC

Reputation: 1324937

9 years, later, the convertion tool not only exists, but has been enhanced with Eclipse 4.33:

Enhance if/else to switch CleanUp

The clean-up to convert an if/else-if/else block into a switch has been enhanced to support String literals and Enum constants.

As before, the if/else-if/else block must have at least 3 blocks and must look for constant expressions except for the last block which can be put into the default case.

  • For a String variable, each if/else expression can check using the String.equals() method.
  • For an Enum, the code simply uses the == operator.
  • Use of the || operator is allowed to check multiple values and corresponds to a fall-through in the resultant switch statement.

To use the clean-up, go to Source > Clean Up > Code Style page and select: Convert if/else if/else chain with 3 blocks min to switch. For example, performing the clean-up on the following:

https://eclipse.dev/eclipse/news/4.33/images/ifelsestring-before.png

will result in:

https://eclipse.dev/eclipse/news/4.33/images/ifelsestring-after.png

Upvotes: 0

zafran
zafran

Reputation: 1

public class question4 {

public static void main(String[] args) {
    int x;
    for(x=0; x<6; x++) {
    if(x==1)
    System.out.println("x is one");
    else if(x==2)
    System.out.println("x is two");
    else if(x==3)
    System.out.println("x is three");
    else if(x==4)
    System.out.println("x is four");
    else
    System.out.println("x is not between 1 and 4");
    }
    }
    
}

Upvotes: -1

Jordi Castilla
Jordi Castilla

Reputation: 26961

First convert this statement to if-else if with CTRL+1 then you will be able to convert the if-else if statement in a switch.

enter image description here

UPDATE:

Depending your Eclipse version, be aware there is a bug to "Convert if-elseto switch" quick assist, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=348179.

This is fixed: Starting 4.3 Milestone 1 Eclipse will support converting if-else statements to switch statements.

Upvotes: 2

Related Questions