Question:
java: Given a String, word, return true if word is a palindrome, false otherwise.?
Lizzy
2012-01-11 15:30:35 UTC
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 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.
Three answers:
McFate
2012-01-11 16:00:43 UTC
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
J
2012-01-11 16:02:08 UTC
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.
2012-01-11 15:49:00 UTC
you are trying to reference a non-static method. This is an instance method but there is no object. Make it -> public static boolean...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...