简体   繁体   English

如何在Java中避免使用多个if-else语句进行vaildation

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

I have lots of multiple if-else statements. 我有很多if-else语句。 For code optimization, I need to write one function for all if else logic. 对于代码优化,我需要为所有if else逻辑编写一个函数。 As of now my code structure is in below. 截至目前,我的代码结构如下所示。

input request is in JSONObject(org.json.simple.JSONObject), which have more than 10 values. 输入请求位于JSONObject(org.json.simple.JSONObject)中,其中包含10个以上的值。

  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, 就像提到的@Emre Acre一样,

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
        } 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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