-2

I'm using eclipse to code a program. Is there any way to simplify this if statement?

 if(departmentName.equalsIgnoreCase("Business Management")){
        departmentName = "SBM";
      }
      if(departmentName.equalsIgnoreCase("Chemical & Life Sciences")){
        departmentName = "SCL";
      }
      if(departmentName.equalsIgnoreCase("Design")){
        departmentName = "SDN";
      }
      if(departmentName.equalsIgnoreCase("Engineering")){
        departmentName = "SEG";
      }
      if(departmentName.equalsIgnoreCase("Oral Health Therapy")){
        departmentName = "SHS(AH)";
      }
      if(departmentName.equalsIgnoreCase("Nursing")){
        departmentName = "SHS(N)";
      }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Bob
  • 157
  • 1
  • 11

3 Answers3

1

You could use a switch statement, and compare the case insensitive department name against your various expected values.

if (departmentName == null) return;
switch(departmentName.toLowerCase()) {
    case "business management":
        departmentName = "SBM";
        break;

    case "chemical & life sciences":
        departmentName = "SCL";
        break;

    // other cases
    default:
        "Not Found";
       break;
}

Note that I do a return in case the department name be null. You may handle null however you want, so long as you don't let your code try to switch on that null value.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

Tim's switch answer is probably the way to go.

But another alternative would be to store those mappings in a Map<String,String> and then get them via

departmentName = theMap.get(departmentName);

To handle the case-insensitive aspect, either store the entries under keys that are all lower case and use

departmentName = theMap.get(departmentName.toLowerCase());

or look at the answers to this question for other ways to do the case-insensitive part.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Since JDK 7, you can use switch statement on String object. See this link. or check @Tim Biegeleisen's answer.

Using switch would not noticeably improve the performance, although your code will be more compact and readable.

In case you are using an older JDK version, I will suggest you to correct your code, since you are doing possible unnecessary checks:

 if(departmentName.equalsIgnoreCase("Business Management")){
     departmentName = "SBM";
 }
 else if(departmentName.equalsIgnoreCase("Chemical & Life Sciences")){
     departmentName = "SCL";
 }
 else if(departmentName.equalsIgnoreCase("Design")){
     departmentName = "SDN";
 }
 else if(departmentName.equalsIgnoreCase("Engineering")){
     departmentName = "SEG";
 }
 else if(departmentName.equalsIgnoreCase("Oral Health Therapy")){
     departmentName = "SHS(AH)";
 }
 else if(departmentName.equalsIgnoreCase("Nursing")){
     departmentName = "SHS(N)";
 }
Oscar Martinez
  • 621
  • 1
  • 8
  • 18
  • You will not notice `switch` optimization until you are not comparing hundreds of `string`s – Oscar Martinez Sep 22 '17 at 09:48
  • More likely thousands, or running the `switch` in a tight loop of thousands of iterations. :-) But that's not the same as it not being there. Good edit. – T.J. Crowder Sep 22 '17 at 09:54