title: Getting A String Index Out Of Range Error
body: <p>I am trying to make a program to find the number of characters of the longest palindrome within a word. What the program does is find all different substrings of the given string and should check if its a palindrome and then the number of characters it has.</p>
  
  <p>Right now it is correctly finding all possible substrings and works if I enter an actual palindrome such as <code>hannah</code>, but if i input something like <code>banana</code>, I get the following error <code>StringIndexOutOfBoundsException</code>.</p>
  
  <p>Here is my code: </p>
  
  <pre><code>import java.util.Scanner;
  
  public class Palindrome {
      public static void main(String[] args) {
  
      Scanner sc = new Scanner(System.in);
      String word;
      String reverseWord;
      int palindromeLength = 0;
  
      System.out.print("Enter A Word: ");
      word = sc.nextLine();
  
      reverseWord = new StringBuffer(word).reverse().toString();
  
      if (reverseWord.equals(word))
      palindromeLength = word.length();
  
      else {
  
          for(int i = 0; i &lt; word.length(); i++) {
  
              for(int j = 1; j &lt;= word.length() - j; j++) {
  
          String substring = word.substring(i, i + j);
          String reverseSubstring = new StringBuffer(substring).reverse().toString();
  
          if (reverseSubstring.equals(substring)) {
  
           if (substring.length() &gt; palindromeLength) {
  
              palindromeLength = substring.length();
  
  
                  }
              }
          }
      }
  }
  
      System.out.println(palindromeLength);
  
      }
  }
  </code></pre>
  
  <p>Anyone know why this is happening and how I could fix the issue?</p>
  
  <p>Thanks!</p>