简体   繁体   中英

Check if a string is a Palindrome (using methods)

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;
}

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;

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 ^

mistake in the first line , it should be :

public static boolean checkPalindrome(String checkString)   

you have to provide the data type before the parameter ^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM