Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

java: Given a String, word, return true if word is a palindrome, false otherwise.?

SO! I pretty much wrote it, but for some reason I keep getting the error saying " String index out of range: 6". I understand what this error means, but I'm not sure how I fix it?

public boolean palindrome(String word) {

int max = word.length();

int start = 0;

boolean rtn=false;

while (start<max){

char left = word.charAt(start);

char right = word.charAt(max);

if (left!=right){

return rtn;

}

if (left==right){

start= start+1;

max=max-1;

rtn = true;

}

}

return rtn;

}

Also if you could tell me how you call a String in main? I think i did it correctly but i'm not sure. I'm using eclipse.

Update:

P.S....please be specific with your answer :/ I'm really new to this so it's hard for me to understand

3 Answers

Relevance
  • McFate
    Lv 7
    9 years ago
    Favorite Answer

    The valid indexes for a String are from 0 through length-1. An index of "length" is off the end of the String. For that reason, when you access word.charAt(max), right after setting max to word.length() it will fail.

    Consider word = "cat". Its length = 3, but its characters are at 0 (c), 1 (a), and 2 (t). word.charAt(3) is invalid. Start max out as word.length() - 1 instead of word.length(), and it should work better.

    ======

    Also, I think you want to just "return true" or "return false" rather than tracking a boolean value in rtn. Consider what happens in your code if you are asked to tell whether "dead" is a palindrome.

    rtn = false to start with.

    Then you compare the first "d" and last "d" and set rtn=true.

    Then you compare the second "e" and third "a" ... and return rtn which is true!

    But that word is not a palindrome.

    As soon as you find ONE character which is mismatched, you can just "return false" right there. If you get through the word and have only found matching characters (you get out of your loop at the bottom) then you can "return true".

    J's example code has a similar issue. If asked whether "Abbe" is a palindrome, it will set isPalindrome equal to the LAST pair of characters compared (the two "b" in the middle) and return true, even though it found a mismatched pair ("a" vs "e") previously.

    Just remember: as soon as you find ONE mismatched pair of characters, you know the word is not a palindrome, and no further checking is necessary.

    @M

  • 9 years ago

    Fixed, and will work in main:

    public static boolean palindrome(String word)

    {

    word = word.toLowerCase().replaceAll(" ", ""); // remove spaces and make case-insensitive

    boolean isPalindrome = true;

    for (int i = 0; isPalindrome && i < word.length() / 2; i++)

    {

    isPalindrome = (word.charAt(i) == word.charAt(word.length() - i - 1));

    }

    return isPalindrome;

    }

    I'll also upload it to pastebin and put the URL in the sources for proper formatting.

  • Anonymous
    9 years ago

    you are trying to reference a non-static method. This is an instance method but there is no object. Make it -> public static boolean...

Still have questions? Get your answers by asking now.