I think the problem is in amount.length()
, you should use text.length()
, since you are looping over the half of text
. The algorithm works fine. Here is a reduced example:
public static boolean palindrome(String text)
{
for (int index = 0; index < text.length() / 2; index++) {
if (text.charAt(index) != text.charAt(text.length() - index - 1)) {
return false;
}
}
return true;
}
Note:
- You forgot to add a
return true
statement, if you don't add one, is possible that thefor
loop finishes and noreturn
statement is reached, which will cause an error. - I would recommend you to follow Java naming conventions. You method should be called like
someMethodName
instead ofSomeMethodName
. This last is used for class names.
Edit:
As @bobbel commented, you could improve this code by assigning text.length()
to a variable and using it inside the for
.