2

How would I do something where I have 3 string variables, string1 string2 and string3 and depending on the user input some of those variables may be empty? I want to compare those variables to another 3 variables I already have set, unless the corresponding string (string1/string2/string3) input is empty

The idea is like this:

If none of them are empty then:

if (o.s1.equals(string1) && o.s2.equals(string2) && o.s3.equals(string3))

if s1 is the only one empty then we only compare the other 2:

if (o.s2.equals(string2) && o.s3.equals(string3))

So the program will not check if that variable is equal if the inputted string is empty

Is there a way to do this that isn't a bunch of nested if statements? I have 5 variables so there would be a lot of statements

For more context what I'm trying to do with this is something like a search function where there are 3 conditions, if all 3 of those fields are filled then it will search for something that meets all 3 conditions, but if one of those are empty then it will only look for entries that meet 2 of the conditions, and if 2 are empty it will search for entries that only satisfy that one condition.

4
  • 1
    First and foremost, don't compare Strings using == or !=. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two object references are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. Commented Jan 28, 2018 at 22:21
  • Next why not simply do if (o.s1.isEmpty()) { or its converse where needed? Commented Jan 28, 2018 at 22:22
  • I'm seeing if there is a simpler way, I can check them individually but any number of strings may be empty, I have 5 variables in my real program so making a nested if else with every single possibility seems like alot of work Commented Jan 28, 2018 at 22:26
  • What is empty? null? ""? Commented Jan 28, 2018 at 23:21

3 Answers 3

2

If by "empty" you mean null, then you could do something like this...

if ((o.s1 == null || o.s1.equals(string1)) &&
    (o.s2 == null || o.s2.equals(string2)) &&
    (o.s3 == null || o.s3.equals(string3))) {
    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could create a helper function which would work regardless of the number of elements you have:

public static boolean areEqual(String[] objectElements, String[] userInputStrings){
       return IntStream.range(0, objectElements.length)
                       .allMatch(x -> userInputStrings[x].isEmpty() ||
                                   userInputStrings[x].equals(objectElements[x]));
}

then call it like this:

boolean result = areEqual(new String[]{o.s1,o.s2, o.s3,o.s4,o.s5 }, 
        new String[]{string1,string2,string3,string4,string5});

Comments

0

I would add a simple utility method for the predicate check that only compares the values if input is not blank:

public static boolean nonBlankEquals(String input, String value) {
     return input == null || input.isEmpty() || input.equals(value);
}

As a result, the client code will be a bit more readable and you don't need to duplicate the variables within the if clause; this will become increasingly important the more these comparisons need to be made.

if (nonBlankEquals(string1, o.s1) 
        && nonBlankEquals(string2, o.s2) 
        && nonBlankEquals(string3, o.s3)
        && nonBlankEquals(string4, o.s4)
        && nonBlankEquals(string5, o.s5)) {
    // all inputs were blank or equal to corresponding values
}

Comments

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.