简体   繁体   中英

how to avoid multiple if-else statements for vaildation in Java

I have lots of multiple if-else statements. For code optimization, I need to write one function for all if else logic. As of now my code structure is in below.

input request is in JSONObject(org.json.simple.JSONObject), which have more than 10 values.

  String s = (String) inputObj.get("test");
  String s1 = (String) inputObj.get("test");
  String s2 = (String) inputObj.get("test");
  String s3 = (String) inputObj.get("test");
        if (s != null && s.trim().isEmpty()) {
            if (s1 != null && s1.trim().isEmpty()) {
                if (s2 != null && s2.trim().isEmpty()) {
                    if (s3 != null && s3.trim().isEmpty()) {
                        if (s4 != null && s4.trim().isEmpty()) {
                           ........
                        } else {
                          return;
                        }
                    } else {
                        return;
                    }
                } else {
                     return;
                }
            } else {
               return;
            }
        } else {
           return;
        }

How to avoid this kind of looping and throw an error message in common method.

Advance thanks.

考虑将所有字符串添加到字符串的数组或ArrayList,并通过其中的每个条目循环,并检查它们是否为空或空。

You can try this.

void main() {
    List<String> sList = new ArrayList<>();
    sList.add(inputObj.get("test"));
    sList.add(inputObj.get("test"));
    sList.add(inputObj.get("test"));
    sList.add(inputObj.get("test"));

    for(String s : sList){
        try {
            checkString(s);
        }catch (Exception e){
            //log or print the exception, however you like
        }
    }
}
void checkString(String s) throws Exception{
    if(s!= null && !s.trim().isEmpty()){
        //doStuff
    }else{
        throw new Exception("String is null or empty !!!");
    }       
}

You should also check this out.

    public Integer checkIsEmapty(String checkingString){
        if(checkingString != null && !checkingString.trim().isEmpty()){
            return 1;
        }
        return 0;
    }

    public String method(){
        String s ="";
        String s1 = "hi";
        String s2 = "java";
        String s3 = null;
        String s4 = null;
       Integer s1i = checkIsEmapty(s);
       Integer s2i = checkIsEmapty(s1);
       Integer s3i = checkIsEmapty(s2);
       Integer s4i = checkIsEmapty(s3);
       Integer s5i = checkIsEmapty(s4);

       Integer total = s1i + s2i + s3i + s4i + s5i;
       switch (total){
           case 1 :
               // To DO
           case 2 :
               // To DO


       }
    }
    in switch used to checking the value, U can pass binary and Integer also 

Like @Emre Acre mentioned,

List<String> sList = new ArrayList<>();
sList.add(inputObj.get("test"));
sList.add(inputObj.get("test"));
sList.add(inputObj.get("test"));
sList.add(inputObj.get("test"));

boolean allDataValid = sList
        .stream()
        .allMatch(s -> s != null && s.trim().isEmpty());

if(allDataValid) {
    ......
} else {
    return;
}
public class YourClass{
    private boolean isBlankDataPresent(JSONObject inputObj, String[] keys) throws Exception {
        for (String key : keys) {
            String input = (String) inputObj.get(key);
            if( input == null || input.trim().isEmpty())
                throw new Exception(key +" is Empty");
        }
        return false;
    }

    public boolean validateData(JSONObject inputObj, String[] keys) throws Exception {
        boolean isBlankDataPresent= isBlankDataPresent(inputObj, keys);
        if (!isBlankDataPresent) {
            // do Your Stuff and return true
        } 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM