title: Bowling Score and Exception Handling
body: <p>This is a code for calculating the Bowling Score,I need help in fixing this error :</p>
  
  <p>Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 </p>
  
  <p>Here is my input(which I store in a text file named bowling.txt).</p>
  
  <p>0 4 5 3 4 2 4 4 3 5 0 8 3 1 2 1 6 4 3 4 </p>
  
  <p>0 P 5 3 4 2 4 4 3 5 0 8 3 1 2 1 6 4 3 4</p>
  
  <p>The game has 10 frames,two tries for each frame,so I thought I need 20 numbers(scores) in the text file.</p>
  
  <p>And here is what I get: </p>
  
  <pre><code>The score is 66
  The score is 77
  Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
      at java.lang.String.charAt(Unknown Source)
      at pin.main(pin.java:77) 
  </code></pre>
  
  <p>N.B: I would give +1 for all useful answers!</p>
  
  <pre><code>import java.io.BufferedReader;   
  import java.io.FileNotFoundException;   
  import java.io.FileReader;    
  import java.io.IOException;    
  
  public class pin     
  {
      static String tries;    
      public static int value(int index) 
      {    
          int i = 0;
          if (tries.charAt(index) == 'T')
              i = 10;
          else if (tries.charAt(index) == 'P')
              i =10 -(tries.charAt(index-2)-'0');
          else
              i = tries.charAt(index)-'0' ;
          return i;
      }
  
      public static void main(String[] args) throws FileNotFoundException, IOException    
      {
          int score = 0;    
          int frameIndex;    
          int i = 0;    
          FileReader fr = new FileReader("C:/Users/PC4599/Desktop/programming/bowling.txt");
          BufferedReader br = new BufferedReader(fr);
          tries = br.readLine();
  
          while (tries != null) 
          {
              score = 0;
              frameIndex = 0;
              i = 0;
              while (frameIndex != 10) 
              {
                  if (tries.charAt(i)=='T') //Strike 
                  {
                      score =(score + 10 + value(i + 2) + value(i + 4));
                      i = i + 2;
                  } 
                  else if (tries.charAt(i+2)=='P') //Spare 
                  {
                      score =(score + 10 + value(i + 4));
                      i = i + 4;
                  }
                  else 
                  {
                      score =(score + (tries.charAt(i)-'0') + (tries.charAt(i + 2)-'0'));//Neither Strike nor Spare 
                      i = i + 4;
                  }
                  frameIndex = frameIndex + 1;
  
              }
  
              System.out.println("The score is "+score);
              tries = br.readLine();    
          }    
          br.close();    
          fr.close();    
      }    
  }
  </code></pre>