title: what is reason of having error like "String index out of range" in java?
body: <p>here is my code.</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  
  public class Solution {
  
      public static void main(String[] args) {
  
          Scanner scan=new Scanner(System.in);
  
          String s;
  
  
          int i;
          int f=1,c,t=scan.nextInt();
  
  
          for(int j=0;j&lt;t;j++)
              {
  
              s=scan.nextLine();
              scan.close();
              c=s.charAt(0)-s.charAt(1);
              for(i=2;i&lt;s.length()-1;i=i+2)//HERE loop is upto second last element
                  {
                  if(s.charAt(i)-s.charAt(i+1)!=c &amp;&amp; s.charAt(i+1)- s.charAt(i)!=c)
                      {
                      f=0;
                      break;
                  }
              }
  
              if(f==1)
                  System.out.print("Funny");
              else
                  System.out.print("Not Funny");
          }
      }
  }
  </code></pre>
  
  <p><strong>ERROR</strong></p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
        at java.lang.String.charAt(String.java:658)
        at Solution.main(Solution.java:22)</p>
  </blockquote>
  
  <p>I tried to search on net but I didn't get answer. </p>