Meg
Meg

Reputation: 119

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.

Upvotes: 1

Views: 1702

Answers (5)

Tino M Thomas
Tino M Thomas

Reputation: 1746

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

Upvotes: 0

Emre Acar
Emre Acar

Reputation: 920

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.

Upvotes: 1

Alanpatchi
Alanpatchi

Reputation: 1199

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

Upvotes: 0

Muthu
Muthu

Reputation: 158

    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 

Upvotes: 0

Aibek
Aibek

Reputation: 358

Consider adding all your strings to array or ArrayList of string, and looping thru each entry in it, and check them for null or emptiness.

Upvotes: 2

Related Questions