mmar
mmar

Reputation: 2020

why return behaves differently here in java?

The return statement in the below code gives me compilation error "Missing Return Statement", where as if i put atlast as single statement it works. what is wrong with my understanding here. please help?

This code gives me "Missing return statement" error at last line.

private static String MixStrings(String str1, String str2) {
    String finalString = "", trimmedString = "", restofStrings = "";

    String[] s1 = str1.split("");
    String[] s2 = str2.split("");

    if (str1.length() > str2.length()) {
        for (int i = 0; i < str2.length(); i++) {
            finalString = finalString + s1[i] + s2[i];
        }

        return finalString + str1.substring(str2.length());
    } else if (str1.length() < str2.length()) {
        for (int i = 0; i < str1.length(); i++) {
            finalString = finalString + s1[i] + s2[i];
        }

        return finalString + str2.substring(str1.length());
    }
}

This code does not show any error and works fine.

private static String MixStrings(String str1, String str2) {
    String finalString = "", trimmedString = "", restofStrings = "";

    String[] s1 = str1.split("");
    String[] s2 = str2.split("");

    if (str1.length() > str2.length()) {
        for (int i = 0; i < str2.length(); i++) {
            finalString = finalString + s1[i] + s2[i];
        }

        finalString = finalString + str1.substring(str2.length());
    } else if (str1.length() < str2.length()) {
        for (int i = 0; i < str1.length(); i++) {
            finalString = finalString + s1[i] + s2[i];
        }

        finalString = finalString + str2.substring(str1.length());
    }

    return finalString;
}

Upvotes: 1

Views: 80

Answers (4)

Sunny
Sunny

Reputation: 3265

In your first code, you added two if conditions that mean if any one of them is true then it will return the case.

But if both are false that means the function does n't return anything. Which is wrong.

So you need to add the return statement in the last after the if.

Like this:

private static String MixStrings(String str1, String str2) {
    String finalString = "", trimmedString = "", restofStrings = "";

    String[] s1 = str1.split("");
    String[] s2 = str2.split("");

    if (str1.length() > str2.length()) {
        for (int i = 0; i < str2.length(); i++) {
            finalString = finalString + s1[i] + s2[i];
        }

        return finalString + str1.substring(str2.length());
    } else if (str1.length() < str2.length()) {
        for (int i = 0; i < str1.length(); i++) {
            finalString = finalString + s1[i] + s2[i];
        }

        return finalString + str2.substring(str1.length());
    }

return "Youe value";    // here is the return if both `if` false

}

Upvotes: 0

mperic
mperic

Reputation: 118

In the case that str1 and str2 are equal in length, you do not have a return statement. Insert an if block that accounts for str1 and str2 being equal in length.

Upvotes: 2

Philip Wrage
Philip Wrage

Reputation: 1559

In your first example you do not have a branch for the condition where the lengths of the two strings are equal. Therefore there is no guarantee that you will encounter a return statement.

Upvotes: 1

user11044402
user11044402

Reputation:

Your first code only returns in the two cases which may both be false at the same time.

You must cover all cases, use an else block, or return after the if/else.

Upvotes: 2

Related Questions