DrunenAlg
DrunenAlg

Reputation: 141

How can I replace this "if" statement with something else?

beginner coder here. So I have this block of code:

public String doOperation(String s1, String s2) {
    if(s2.compareTo("") == 0) return s1;
    else return s2;
}

Just a rough draft, but essentially I'm saying to return s2 unless it's an empty string, then return s1 instead. Is there any way I can accomplish this without an if statement? For the assignment, I'm not supposed to use if statements, switch statements, while loops, exception handling, or pretty much anything else that is just a sneaky way to get around an "if" statement.

I'm trying to figure out a way to do this through polymorphism but I also don't want to be creating a ton of new things just to accomplish this tiny task. Any ideas/help? Thanks!

Upvotes: 3

Views: 176

Answers (6)

Izruo
Izruo

Reputation: 2276

public String doOperation(String s1, String s2) {
    int mask = 1 / (1 + s2.length());
    return s1.substring(0, s1.length() * mask) + s2);
}

The mask will be 0 for any nonempty String due to integer-arithmetic and 1 for an empty String, as 1 / 1 = 1.

In the second line the 0 mask will 'nullify' the length of the substring, leaving it empty and the appended s2 is the only thing that is returned.

On the other hand the 1 mask will not change the value of s1.length() and the appended s2 is empty anyway (as we got the 1 mask), so the only thing returned is s1 from beginning to end.

Upvotes: 1

Devendra Lattu
Devendra Lattu

Reputation: 2802

You should always make sure that you handle null scenarios and exception handling first.

public String doOperation(String s1, String s2) {
    if (s2 == null) {
        System.out.println("Error Msg: null value");
        throw new NullPointerException();
    }
    return s2.isEmpty() ? s1 : s2;
}

Update 1: For this particular scenario using s2.isEmpty() is slightly faster or has a better coding style when compared to s2.equals(""), or s2.length() == 0, or s2.compareTo("") == 0

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201439

Since everyone else has covered the ternary (or conditional operator ? :), you could also do it with a switch like

public String doOperation(String s1, String s2) {
    switch (s2.length()) {
    case 0: return s1;
    }
    return s2;
}

Upvotes: 1

Miquel
Miquel

Reputation: 15675

Looks like a bit of a brain teaser.. This is awful code, but should do what you want...

return s1.substring(Math.min(s1.length()-1, s1.length() * s2.length()) + s2

So, if s2's length is 0, you'll print s1 starting at position 0, followed by s2, which is empty

If s2's length is not 0, you'll print s1 starting at the last position (i.e. nothing) followed by s2

Oh boy, I feel dirty now :)

Upvotes: 2

RAZ_Muh_Taz
RAZ_Muh_Taz

Reputation: 4089

It's called a Ternary operation, super useful

public String doOperation(String s1, String s2) {
    return s2.compareTo("") == 0 ? s1 : s2;
}

Upvotes: 1

Vladimir Parfenov
Vladimir Parfenov

Reputation: 665

You can use ternary operator:

return s2.isEmpty() ? s1 : s2;

Upvotes: 3

Related Questions