java - Check which (combinations) of parameters are null -
suppose have 4 variables
string a; string b; string c; string d;
i want check if individual variable or combination of variable not null , act accordingly.
for example 1 way using if-else way
if(a!=null && b == null && c == null && d == null) { //dosomething } else if(a==null && b!= null && c == null && d == null) { //dosomething } else if(a==null && b!= null && c == null && d == null) { //dosomething } else if(a==null && b== null && c != null && d == null) { //dosomething } ...... //similarly combination of 2 variables if(a!=null && b != null && c == null && d == null) { //dosomething } else if(a!=null && b== null && c != null && d == null) { //dosomething } else if(a!=null && b== null && c == null && d != null) { //dosomething } ...... //and on //similarly combination of 3 variables if(a!=null && b != null && c != null && d == null) { //dosomething } else if(a!=null && b== null && c != null && d != null) { //dosomething } else if(a!=null && b== null && c == null && d != null) { //dosomething } ....
how achieve kind of situation switch
don't accept null thought of using queue dont allow null values need have key variable can value , manipulate something. appreciated
you build additional variable achieve switch statement:
int switchvar = 0; if (a == null) {switchvar += 1;} if (b == null) {switchvar += 10;} if (c == null) {switchvar += 100;} if (d == null) {switchvar += 1000;}
and can use
switch (switchvar) { case(1): //only ==null ... case(101): //a == null , c == null .... case(1011): //a,b,d null .... case(1111): // variables null }
Comments
Post a Comment