Check if a string is a Palindrome (using methods)

时间:2015-10-30 21:48:13

标签: java eclipse string methods boolean

I have no idea why the following method is not working. It shows:

Palindrome.java:97: error: <identifier> expected

The method takes a parameter which is a String and should return true or false if the provided String is a palindrome.

public static boolean checkPalindrome(checkString) {
    boolean test = true;
    int left = 0;
    int right = checkString.length() - 1;
    while (left < right && test) {
        if (checkString.charAt(left) != checkString.charAt(right)) {
            test = false;
        }
        right--;
        left++;
    }
    return test;
}

3 个答案:

答案 0 :(得分:3)

Problem is this line

public static boolean checkPalindrome(checkString)

Should be

public static boolean checkPalindrome(String checkString)

Just a suggestion but you can also reduce the variables that you are using

int len = checkString.length();
for (int i = 0; i < len / 2; i++) {
    if (checkString.charAt(i) != checkString.charAt(len - i -1)) {
        return false;
    }
}
return true;

答案 1 :(得分:3)

I think you made a mistake in the first line , it should be :

public static boolean checkPalindrome(String checkString)   

you have to provide the data type before the parameter ^

答案 2 :(得分:3)

第一行出错,应该是:

BasicNetwork.performRequest: Unexpected response code 404. 

您必须在参数^

之前提供数据类型