0

I have a java method which is supposed to check 20 inputs parameters for empty and return a message

Example:

public String CheckEmpty(String a,String b,String c, String d,String e, String f, String g, String i,String j, String k,.....) {
    String str="";
    if(a.isEmpty()){
        str= "a is empty";
    }
    if(b.isEmpty()){
        str= "b is empty";
    }
    return str;
} 

I need to check for the if condition for all inputs? there are around 20 inputs or is there any efficient way of doing the same check in java ?

Please advise.

5
  • The Strings a,b,c,d etc are coming in request parameters individually. Commented Jun 26, 2015 at 6:55
  • Right and then cylcle through all items in a for-loop. By this the function does not even need to know, how many parameters you passed. Also right now you should think about the return value. In your way if a and b are empty the function will only return "b is empty" ... Commented Jun 26, 2015 at 6:55
  • @user3492471 then what's the problem of constructing an array from those strings? Commented Jun 26, 2015 at 6:56
  • Imho better solution is not to pass 20 arguments to a method but varargs, or maybe array of strings? The less arguments a method has the better and easier to read :) Commented Jun 26, 2015 at 6:57
  • Also since the strings are coming in request parameters individually, why don't you call the same method on them? Commented Jun 26, 2015 at 6:58

4 Answers 4

7

It's better to use variable arguments like this:

public String checkEmtpy(String... args) {
    for(int i=0; i<args.length; i++) {
        if(args[i].isEmpty()) {
            return ((char)('a'+i))+" is empty";
        }
    }
    return "";
}

For example, checkEmtpy("aaa", "b", "", "ddd") returns c is empty.

Sign up to request clarification or add additional context in comments.

3 Comments

You were a bit faster than me :), btw the best solution for this problem.
But what happens if you pass checkEmpty("aaa", "", "cc", ""). You will only return "b is empty" even though the last parameter is empty too. It probably depends on what needs to be achieved by the return value ...
It is better to return a map with parameters and related messages.
0

Use a Map(<String>, <String>) as parameter, then do the checks in a loop.

The keys of the map would be the variable names, the values of the map would be the values that you want to check.

Comments

0

Try this:

public String CheckEmpty(List<String> argumentList) {
    for(Iterator<String> i = argumentList.iterator(); i.hasNext(); ) {
        String item = i.next();
        System.out.println(item);
    }
        return item;
}

2 Comments

Why iterator? for( : ) is shorter. Why does method returns nothing?
@cybersoft, it shows an approach how to handle multiple arguments using list, returning something was not the main purpose.
-1
  1. Take input parameters as a string array
  2. Then loop through it and check for each element's boolean representation

    for(int i = 0; i<20;i++)  
    {  
        if(null == stringInputArray[i])  
        {
        //Using **i** you can take the current empty element's index and 
          do your stuff here...
        }  
    }
    

UPDATE:

Change code to check whether element is null.

2 Comments

Boolean.valueOf("asd") also returns false. see java docs
Sorry :(. and thanks also. changed code to use null check instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.