I am writing a program that reads text but must ignore case, spaces and punctuation while evaluating a string for palindrome. When I use the following the case issue is solved.

String str = "";
if (str.toLowerCase().charAt(LHS) != str.toLowerCase().charAt(RHS))

...but when I use the following I get IndexOutOfBoundsException error can anyone explain why?

String str = "";
if (str.toLowerCase().replaceAll("\\w", "").charAt(LHS) != str.toLowerCase().replaceAll("\\w", "").charAt(RHS))

In your new String, either "LHS" or "RHS" represents an Index which is no longer valid. Since replacing whitespace with "" makes your string shorter, your index is no longer valid in the new string. In your new string, which is shorter than the old string, more than likely the "RHS" is greater than the highest index

edit:

to add to that, the above is the reason why you should really be making a custom method which simply goes character by character through your string and ignores whitespace. The replaceAll technique isn't bad, but after you do that, you should use a for loop to check the two strings to see if they're the same.

In your new String, either "LHS" or "RHS" represents an Index which is no longer valid. Since replacing whitespace with "" makes your string shorter, your index is no longer valid in the new string. In your new string, which is shorter than the old string, more than likely the "RHS" is greater than the highest index.

I thought I was simply setting conditions on a string on both sides of the equal sign but I was wrong. Any thoughts on how to apply removing whitespace and punctuations? Also I need to consider both whitespaces and punctuations.

What you did by calling replaceAll is construct a new String with all of the whitespace removed. The returned String was smaller than your original String, hence when you used the original index, it was larger than the size of the String. In my first post I gave a suggestion: use replaceAll (like you used it) to remove the whitespace. Then use a loop to check the strings to see if they're palindromes. Simple if statements can ignore punctuation ..

What you did by calling replaceAll is construct a new String with all of the whitespace removed. The returned String was smaller than your original String, hence when you used the original index, it was larger than the size of the String. In my first post I gave a suggestion: use replaceAll (like you used it) to remove the whitespace. Then use a loop to check the strings to see if they're palindromes. Simple if statements can ignore punctuation ..

I did two things, I did as you suggested and I got the result I needed (in terms of whether or not the string is palindrome). While doing so joined all the characters, I really need to check its condition for these specifications. Secondly, I am trying the long way to just check the condition. While the program compiled and produced a result it did not appear to have an impact. Can you critique please.

// Recursive method   
      public static String isPalindrome(String str) {
         int LHS = 0;                              // the first char of string
         int RHS = str.length() -1;                // the last char of string
         while (LHS < RHS) {                       // continue until they reach center
         // consider case sensitive inputs and converts to one case type
            //if (str.toLowerCase().charAt(LHS) != str.toLowerCase().charAt(RHS))
if(!(Character.isDigit(str.toLowerCase().charAt(LHS)) || Character.isLetter(str.toLowerCase().charAt(LHS))) !=
!(Character.isDigit(str.toLowerCase().charAt(RHS)) || Character.isLetter(str.toLowerCase().charAt(RHS))))
               return palindromeNo;
            LHS++;                                 // index toward the center of string from LHS
            RHS--;                                 // decrement toward the center from RHS
         }
         return palindromeYes;
      }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.