簡體   English   中英

如何在Java中避免使用多個if-else語句進行vaildation

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

我有很多if-else語句。 對於代碼優化,我需要為所有if else邏輯編寫一個函數。 截至目前,我的代碼結構如下所示。

輸入請求位於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;
        }

如何避免這種循環並在常用方法中拋出錯誤消息。

謝謝。

考慮將所有字符串添加到字符串的數組或ArrayList,並通過其中的每個條目循環,並檢查它們是否為空或空。

你可以試試這個。

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 !!!");
    }       
}

您也應該檢查這個了。

    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 

就像提到的@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