0

Kindly help me if there is any other easy way to optimise/implement the code on the below possibilities. Variables like home, bill, reg can either be empty string("") or null depending on the scenario.

My code

public void helloWorld()
{
    List items=new ArrayList();
    items.add("1"); // dummy list
    String home=""; // can be null or empty
    String bill=null;  // can be null or empty
    String reg="";  // can be null or empty
    if(!items.isEmpty())
    {
        System.out.println("List is not null");
        if(home==null&&bill==null&&reg==null) 
        {
            System.out.println("home is null");
            System.out.println("bill is null");
            System.out.println("reg is null");
        }
        if(home==null&&bill==null&&reg!=null)
        {
            System.out.println("home is null");
            System.out.println("bill is null");
            System.out.println("reg is not null");
        }
        if(home==null&&bill!=null&&reg==null)
        {
            System.out.println("home is null");
            System.out.println("bill is not null");
            System.out.println("reg is null");
        }
        if(home==null&&bill!=null&&reg!=null)
        {
            System.out.println("home is null");
            System.out.println("bill is not null");
            System.out.println("reg is not null");
        }
        if(home!=null&&bill==null&&reg==null)
        {
            System.out.println("home is not null");
            System.out.println("bill is null");
            System.out.println("reg is null");
        }
        if(home!=null&&bill==null&&reg!=null)
        {
            System.out.println("home is not null");
            System.out.println("bill is null");
            System.out.println("reg is not null");
        }
        if(home!=null&&bill!=null&&reg==null)
        {
            System.out.println("home is not null");
            System.out.println("bill is not null");
            System.out.println("reg is null");
        }
        if(home!=null&&bill!=null&&reg!=null)
        {
            System.out.println("home is not null");
            System.out.println("bill is not null");
            System.out.println("reg is not null");
        }
    }
    else
    {
        System.out.println("List is null");
    }
}

Please advise. Thanks in advance.

user3492471
  • 93
  • 2
  • 16
  • 3
    Why not check and print each variable separately? it would be much cleaner that way. – Dragondraikk Jul 09 '15 at 15:26
  • 1
    If `home` is `null`, you'll print `home is null`, otherwise you'll print `home is not null`. This is independent of the values of your other variables. – Sotirios Delimanolis Jul 09 '15 at 15:27
  • **Warning**: you are using *raw types* (`List` and `ArrayList`). Never do that, always specify the necessary *type arguments*. (Even if this is a simplified example, don't use raw types.) – MC Emperor Feb 03 '22 at 13:30

4 Answers4

3

Just check home, bill, and reg individually, because they have no dependency with each other.

public void helloWorld()
{
    List items=new ArrayList();
    items.add("1"); // dummy list
    String home=""; // can be null or empty
    String bill=null;  // can be null or empty
    String reg="";  // can be null or empty
    if(!items.isEmpty())
    {
        System.out.println("List is not null");
        System.out.println( home == null ? "home is null" : "home is not null" );
        System.out.println( bill == null ? "bill is null" : "bill is not null" );
        System.out.println( reg == null ? "reg is null" : "reg is not null" );

    }
    else
    {
        System.out.println("List is null");
    }
}
yas
  • 3,520
  • 4
  • 25
  • 38
1

As proposed just check each variable instead of listing all permutations

    if(home==null) 
    {
        System.out.println("home is null");
    }
    else
    {
        System.out.println("home is not null");
    }
    if (bill==null)
    {
        System.out.println("bill is null");
    }
    else
    {
        System.out.println("bill is not null");
    }
    ...
nucleon
  • 1,128
  • 1
  • 6
  • 19
1
 public void helloWorld() {
        List items = new ArrayList();
        items.add("1"); // dummy list
        String home = ""; // can be null or empty
        String bill = null;  // can be null or empty
        String reg = "";  // can be null or empty
        if (!items.isEmpty()) {
            System.out.println("List is not null");
            System.out.println("home is " + (StringUtils.isEmpty(home) ? "null" : "not null"));
            System.out.println("bill is " + (StringUtils.isEmpty(bill) ? "null" : "not null"));
            System.out.println("reg is " + (StringUtils.isEmpty(reg) ? "null" : "not null"));
        } else {
            System.out.println("List is null");
        }
    }

StringUtils.isEmpty() function can be used to check both null and empty values.

0

According to your comment on other answer, you need to do some actions...

So here is an example of how you could reuse the code to do different actions according to the value of different strings:

public void helloWorld() {
    List items = new ArrayList();
    items.add("1"); // dummy list
    String home = ""; // can be null or empty
    String bill = null; // can be null or empty
    String reg = ""; // can be null or empty


    switch (validateInputString(home)) {
    case 1:
        //doAction 1;
        break;
    case 2:
        //doAction 2;
        break;
    case 3:
        //doAction 3;
        break;

    default:
        break;
    }

}

public int validateInputString(String toValidate){
    if(toValidate==null)
        return 1;

    if(toValidate.equals(""))
        return 2;


    return 3;
}

You could do this for every string, and something quite similar for the list...

melli-182
  • 1,216
  • 3
  • 16
  • 29