Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8469441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:13:45+00:00 2026-06-10T16:13:45+00:00

This is a code for calculating the Bowling Score,I need help in fixing this

  • 0

This is a code for calculating the Bowling Score,I need help in fixing this error :

Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 0

Here is my input(which I store in a text file named bowling.txt).

0 4 5 3 4 2 4 4 3 5 0 8 3 1 2 1 6 4 3 4

0 P 5 3 4 2 4 4 3 5 0 8 3 1 2 1 6 4 3 4

The game has 10 frames,two tries for each frame,so I thought I need 20 numbers(scores) in the text file.

And here is what I get:

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) 

N.B: I would give +1 for all useful answers!

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();    
    }    
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:13:47+00:00Added an answer on June 10, 2026 at 4:13 pm

    It seems like the while (tries != null) loop is running three times. Your input file probably has an extra line at the end, which might just be whitespace.

    Given the format of your data input, a minimal correct line of scores would have 23 characters (12 strikes separated by spaces), so you could change that check to something like while ((tries != null) && (tries.length() >= 23)). That should solve this problem and seems like a pretty reasonable thing to do anyway. (If I’m not remembering how to score bowling correctly, you can probably make the appropriate adjustments.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have never used ruby but need to translate this code to java. Can
This code below allows me to find the word error in all my files
This code gives me this error:Cannot implicitly convert type ArrayList[] to ArrayList[][] at this
This code fails on some machines: // Parse error: syntax error, unexpected '[' ...
I was calculating the Fibonacci sequence, and stumbled across this code, which I saw
See i have this following code below, at line no.9 i'm calculating some dates
I have a GUI Java code for calculating the range of data for example
Another jquery calculation question. I've this, which is sample code from the plugin site
This code is the core of a much larger script that works great in
This code disabled mouse scroll functionality, when i click on the document. $(document).on(click, function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.