简体   繁体   中英

Making a palindrome checker I keep getting a string index out of range error for half of it, how do i fix?

So I'm making a palindrome checker, what it does is that it will go through a statement, lets say for example "racecar" if it is a palindrome it will say true which in this case it is because racecar backwards is racecar, but if it was "ratecar" as the input then it would say false because ratecar is different from racetar, and I keep getting an error like a string index out of range type of error.
I've made a new page and tried just using this:

    class Main 
    {
      public static void main(String[] args) 
      {

        String str = new String("Go Hang A Salami, I'm a Lasagna Hog!");
           str = str.toLowerCase();
           str = str.replaceAll(" ","");
           str = str.replaceAll("[^a-zA-Z ]","");
           System.out.println(str);
      }
    }

and it does what it needs to do and give the output: "gohangasalamiimalasagnahog" but the checker I made which is shown below isn't working when I merge what I have above with it.

    class Main 
    {
      public static void main(String[] args) 
      {

        System.out.println("This is A-A-Ron's palindrome checker");
        System.out.println(isPalindrome("Go Hang A Salami, I'm A Lasagna Hog!")); 
      }

          public static boolean isPalindrome(String str)
        {
           int len = str.length();
            int i, j;

            j = len - 1; 

            for (i = 0; i <= (len - 1)/2; i++)
            {

                    str = str.toLowerCase();
                    str = str.replaceAll(" ","");
                    str = str.replaceAll("[^a-zA-Z ]","");
                    System.out.println(str);
                    if(str.charAt(i) != str.charAt(j))
                      {
                        return false;
                      }
                      j--;


            }
            System.out.println(str);
            return true; 
        }
`````
It should give the output of:
"This is A-A-Ron's palindrome checker
gohangasalamiimalasagnahog
true"

But, it is giving me this:
"This is A-A-Ron's palindrome checker
gohangasalamiimalasagnahog
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 35
    at java.lang.String.charAt(String.java:658)
    at Main.isPalindrome(Main.java:28)
    at Main.main(Main.java:9)
exit status 1"

What do I do to fix it?

Your problem is pretty simple. You get the len from the original string but you replace some of the string inside the loop. Just make it like this

public static boolean isPalindrome(String str) {

  str = str.toLowerCase();
  str = str.replaceAll(" ", "");
  str = str.replaceAll("[^a-zA-Z ]", "");
  System.out.println(str);
  int len = str.length();
  int i, j;

  j = len - 1;

  for (i = 0; i <= (len - 1) / 2; i++) {
    if (str.charAt(i) != str.charAt(j)) {
      return false;
    }
    j--;
  }
  System.out.println(str);
  return true;
}

You are storing the length of the original string (36) and then removing the white space from it and trying to access that index, however the new string is shorter and you get the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM