Search code examples
javafor-looppalindrome

Can't figure out why a for loop won't run


I've tried embedding println checks within and above/below the for loop in this, but I can't find a problem with the loop itself.

Function is part of a simple palindrome checker.

public static String checkPalindrome(String x) {
    x=x.toLowerCase();
    String trimmedX = x.replaceAll("\\W","");
    String reverseX = new StringBuilder(trimmedX).reverse().toString();
    int length= trimmedX.length();
    for (int i=0;i>length;i++){
        if (trimmedX.charAt(i)!=reverseX.charAt(i)) {
            String z = "It is not a palindrome.";
            return z;
        }
        String z = "It is a palindrome.";
    }
    return z;
}

I originally had Z set as "it is" above the for loop, but in trying to fix things I've moved it inside the for loop, which confirmed the loop not running.


Solution

  • Perhaps you should inspect the loop more closely:

    for (int i=0;i>length;i++){
    

    I think there is an issue with the check on i.