0

My question is simply:


  • Is it possible else statements are not guaranteed in some instances(excluding when, break, continue or return is called beforehand)?

For example: in this code an else statement follows at the end with a return call, but the error: [This method must return a result of type Boolean]. It is as if there is a possibility the else will not be called if the "if" statement is false.

public static boolean palindrome(String s){
    if (s.length() == 1 ){
    return false;
    }
    else if (s.length() == 2 ){
      if(s.toLowerCase().trim().charAt(0) >= 'a' && s.toLowerCase().trim().charAt(0) <= 'z' && s.toLowerCase().trim().charAt(0) == s.toLowerCase().trim().charAt(s.length())){
        return true;
      }  
      else return false;
    }
    else if(s.toLowerCase().trim().charAt(0) >= 'a' && s.toLowerCase().trim().charAt(0) <= 'z'){
      if(s.toLowerCase().trim().charAt(s.length()-1) >= 'a' && s.toLowerCase().trim().charAt(s.length()-1) <= 'z'){
        if(s.toLowerCase().trim().charAt(0) == s.toLowerCase().trim().charAt(s.length()-1))
          return true && palindrome(s.substring(1,s.length()-1));
      }
      else return palindrome(s.substring(0, s.length()-1));
    }
  }
  • Close the else if loop brackets before you start with the else part :) – Govind Madhu Sep 26 '16 at 07:40
  • 1
    Oh, gosh, Can you please adjust the code to do a single `s.toLowerCase().trim()` at the beginning? You will see better the matching (or not matching) braces. – Adrian Colomitchi Sep 26 '16 at 07:40
  • Thanks I see that now! Thank you for keeping the community up and running Madhu. I know Adrian, I want to but this is a class assignment and my Prof. specified no locals! –  Sep 26 '16 at 07:43
  • 1
    "is a class assignment and my Prof. specified no locals!" Damn'd this globalisation! Next thing you know: your prof will apply for a quota of those visas to bring in programmers from abroad :) – Adrian Colomitchi Sep 26 '16 at 07:51
  • Wordplay.. your syntax impresses and caresses. –  Sep 26 '16 at 08:25

1 Answers1

1

Your else return palindrome(s.substring(0, s.length()-1)); statement corresponds with the inner if(s.toLowerCase().trim().charAt(s.length()-1) >= 'a' && ... statement, so your outer if-else-if block has no else statement. You should add one (or move that else statement there, if that's what the logic requires).

For example, you can change

...
else if(s.toLowerCase().trim().charAt(0) >= 'a' && s.toLowerCase().trim().charAt(0) <= 'z'){
  if(s.toLowerCase().trim().charAt(s.length()-1) >= 'a' && s.toLowerCase().trim().charAt(s.length()-1) <= 'z'){
    if(s.toLowerCase().trim().charAt(0) == s.toLowerCase().trim().charAt(s.length()-1))
      return true && palindrome(s.substring(1,s.length()-1));
  }
  else return palindrome(s.substring(0, s.length()-1));
}

to

...
else if(s.toLowerCase().trim().charAt(0) >= 'a' && s.toLowerCase().trim().charAt(0) <= 'z'){
  if(s.toLowerCase().trim().charAt(s.length()-1) >= 'a' && s.toLowerCase().trim().charAt(s.length()-1) <= 'z'){
    if(s.toLowerCase().trim().charAt(0) == s.toLowerCase().trim().charAt(s.length()-1))
      return palindrome(s.substring(1,s.length()-1));
  }
} else {
    return palindrome(s.substring(0, s.length()-1));
}

Of course, that will leave you with some execution paths with no return statement - for example, when the condition of if(s.toLowerCase().trim().charAt(0) == s.toLowerCase().trim().charAt(s.length()-1)) is false, you have no return statement.

Instead of matching an else clause to each if statement, you can consider adding a final return statement as the last statement of the method. It could simplify your code.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Oh gosh! the correspondence! So else is still a stable statement, I had a midlife crisis and lost hope in 'else'. Glad to know I can still trust him. –  Sep 26 '16 at 07:41
  • @JasonIvey You brute, see what you've done with your sloppy coding style? You pushed yourself into mid-life crisis. If you continue on the same path, you'll get suicidal at some point. Seriously man, style does matter not only in fashion. – Adrian Colomitchi Sep 26 '16 at 07:46
  • I know... I declared an array of int's like this yesterday (int ar[])... It works but.... but the self harm is getting worse... soon i'm going to use recursion for complex equations because I like the slower algorithms.. makes me feel like humans are still smarter than machines... This path I am on will eventually lead to all my variables being x, y, and z... Then suicide would just be a release from the this spinning blue rock, oh this this carbon rich spinning blue rock... Maybe that's how humanity ended... misplaced else statements... –  Sep 26 '16 at 08:23