使用substring()和charAt()检查String是否为Palindrome

时间:2014-02-04 02:30:17

标签: java

我的代码应该要求用户输入一个字符串,然后使用charAt()substring()方法检查是否是Palindrome。我相信,我已经做了一切正确但NetBeans正在编译错误。

当我运行我的代码时,NetBeans告诉我:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
    at palindrome.Palindrome.methodA(Palindrome.java:31)
    at palindrome.Palindrome.main(Palindrome.java:27)
Java Result: 1

所以,基本上它说我错过了一个回复声明?我不明白这是怎么可能的。

任何帮助都会非常感谢大家。感谢。

package palindrome;

import java.util.Scanner;

public class Palindrome {

    /**
     * Program asks user to enter a string.
     */
    public static void main(String[] args) {
        String userInput; //user-inputted String
        String result;

        //set up instance of Scanner for user input
        Scanner scan = new Scanner(System.in);

        //ask user for input
        System.out.print("Please enter a String: ");
        userInput = scan.nextLine();

        //run methodA
        result = methodA(userInput);

    }

    public static String methodA(String inString) {
        /*
         * This method checks the user-inputted String against a backward copy
         * of itself using only String and Character methods.
         */

        //next two int variables used as int pointers for first letter of String
        //and last letter of user-inputted String
        int begPoint = 0;
        int endPoint = inString.length() - 1;

        //define Strings to return to main method
        String isPal = (inString + " is a palindrome!");
        String notPal = (inString + " is not a palindrome.");

        while (begPoint < endPoint) {
            //two substrings defined to test String character for character
            String firstChar = inString.substring(begPoint, begPoint + 1);
            String lastChar = inString.substring(endPoint, endPoint + 1);

            //algorithm continues step by step with begPoint going up one and 
            //endPoint decreasing by one
            begPoint++;
            endPoint--;

            //basically here I am trying to check the characters of the user-
            //inputted Strings using the charAt methods but I'm lost when I
            //get to this point
            if (inString.charAt(begPoint) == inString.charAt(endPoint)) {
                return isPal;
            } else {
                return notPal;
            }
        }
    }
}

在得到一些答案之后 - 我现在已将我的while语句改为:

while(begPoint<endPoint) {
    //two substrings defined to test String character for character
    String firstChar = inString.substring(begPoint, begPoint + 1);
    String lastChar = inString.substring(endPoint, endPoint + 1);

    //basically here I am trying to check the characters of the user-
    //inputted Strings using the charAt methods 
    if (inString.charAt(begPoint) == inString.charAt(endPoint)) {
        return isPal;
    } else {
        return notPal;
    }

    //algorithm continues step by step with begPoint going up one and 
    //endPoint decreasing by one
    begPoint++;
    endPoint--;
}

然而,现在它告诉我(begPoint ++)是一个无法访问的语句。

2 个答案:

答案 0 :(得分:1)

在此代码中

if(inString.charAt(begPoint)==inString.charAt(endPoint))
{
       return isPal;
}
else
{
       return notPal;
} 

你基本上是在循环的第一次迭代中返回。

如果它不是Palindrome,那么立即返回是有道理的,但是其他方面你需要继续循环。

如果未输入while,则代码将无法编译。也许会返回notPal

另外(感谢ArtOfWarfare),您需要移动

//algorithm continues step by step with begPoint going up one and 
//endPoint decreasing by one
    begPoint++;
    endPoint--;

到你的while循环结束,即在你完成if语句之后。

答案 1 :(得分:1)

你的问题是你在检查charAts之前正在移动begPoint和endPoint。在使用方法charAt后,将代码前进的begPoint和endPoint移动到。