Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

java - Missing return Statement, Recursive method

I keep getting the error

Palindrome.java:36: error: missing return statement } ^ 1 error

when i try to compile, for the lab I have to have a recursive method which returns true if the input string is a palindrome ignoring non letter characters. So what is the problem and how do i go about fixing it.

package lab07;

import java.util.Scanner;

public class Palindrome{
   public static void main(String[] args){
      Scanner input = new Scanner(System.in);
      while ( input.hasNext()){
        String line =  normalise(input.next());
      System.out.println(isPalindrome(line));
      }
}
   public static String normalise(String line){
    String s = "";
    char[] chars = line.toCharArray();
    for (int i = 0; i < chars.length; i++){
        if ( Character.isLetter(chars[i]))
        s += Character.toLowerCase(chars[i]);
}
    return s;
   }
   public static boolean isPalindrome(String line){
    if ( line.length() > 1 ){
        if (line.charAt(0) == line.charAt(line.length()-1)){
            isPalindrome(line.substring(1, line.length()-1));
        }
        else
            return false;

    }
    else
        return true;
   }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're missing the return statement inside the inner if condition

   public static boolean isPalindrome(String line){
    if ( line.length() > 1 ){
        if (line.charAt(0) == line.charAt(line.length()-1)){
            isPalindrome(line.substring(1, line.length()-1));
               // MISSING RETURN HERE
        }
        else
            return false;

    }
    else
        return true;
   }

There needs to be a return for every possible condition.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...