Bharat
Bharat

Reputation: 11

My method can't return boolean

I am newbie in java.

Here is my code:

public boolean endsLy(String str) {
  if(str.length()>=2){
    if(str.substring(str.length()-2).equals("ly")) return true;
  }
  else return false;
}

but compiler gives:

Error:  public boolean endsLy(String str) {          
This method must return a result of type boolean

Possible problem: the if-statement structure may theoretically allow a run to reach the end of the method without calling return.
Consider adding a last line in the method return some_value; so a value is always returned.

Upvotes: 0

Views: 913

Answers (6)

Anirban Pal
Anirban Pal

Reputation: 529

Avoid multiple false return statement as return value is true only for condition if(str.substring(str.length()-2).equals("ly")). Following code is for reference.

 public boolean endsLy(String str) {


      if(str.length()>=2){

        if(str.substring(str.length()-2).equals("ly")) 
            return true;

      }
    return false;

    }

Upvotes: 0

Mad Physicist
Mad Physicist

Reputation: 114230

You are not handling the branch where (str.length()>=2, but !str.substring(str.length()-2).equals("ly"). Remove the else from the final return statement:

public boolean endsLy(String str) {


  if(str.length()>=2){

    if(str.substring(str.length()-2).equals("ly")) return true;

  }
  return false;

}

An even simpler alternative (also less prone to the kind of error you are having), is to have only a single return statement:

public boolean endsLy(String str) {
    return str.length()>=2 && str.substring(str.length()-2).equals("ly");
}

Upvotes: 6

Dali
Dali

Reputation: 135

because you do not have a boolean value is returned in case !str.substring (str.length () -2). equals ("ly")

if you want to check that your chain is composed of four characters in the last two are "ly" you can use the following codes:

public boolean endsLy(String str) {
    if (str.length() == 4 && str.endsWith("ly"))
        return true;
    return false;
}

Upvotes: 0

Talha Masood
Talha Masood

Reputation: 993

This error means that there might be a possibility that the function will not return anything under some circumstances.

so if this condition => if(str.length()>=2) stands true the code will enter into it. Now if this condition is false => if(str.substring(str.length()-2) the function will have nothing to return. So this is a wise thing that the error prompted.

This means that not all conditions in this function return a bool value. There is a chance that conditions may occur when function does not have anything to return.

Upvotes: 0

user2336315
user2336315

Reputation: 16067

Or simply :

public static boolean endsLy(String str) {
     return str.length()>= 2 && str.substring(str.length()-2).equals("ly");
}

You might also check if the String is not null.

return str != null && str.length()>= 2 && str.substring(str.length()-2).equals("ly");

Upvotes: 3

semirturgay
semirturgay

Reputation: 4201

here a correction:

 public boolean endsLy(String str) {
     if(str.length()>=2){

        if(str.substring(str.length()-2).equals("ly"))
         return true;
         else
         return false;

      }
      else{ 
      return false;
      }

}

Upvotes: 0

Related Questions