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

  • Home
  • SEARCH
  • 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 8866993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:55:09+00:00 2026-06-14T16:55:09+00:00

I’ve run into the following problem with my program (only on attempting to run

  • 0

I’ve run into the following problem with my program (only on attempting to run it, builds fine):

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: 
String index out of range: 57
at java.lang.String.substring(String.java:1907)
at Question7.main(Question7.java:68)

I know there are similar questions on the site, but I’m going through the steps in my head and can’t figure out where this is going wrong.
I don’t think the context of the code/question asked is very important; I believe the problem has something to do with the following lines:

else if (s1.substring(i,i+1).matches("[0-9]"))

if (counthyphen == 3 && countdigits == 9 && (s1.substring(i, i+1).matches("[0-9]") || s1.substring(i, i+1).matches("X")))

But please, have a look for yourself. Help would be much appreciated!

public class Question7
{
public static void main(String args[])
{
    //Declare and initialize.
    String s1 = new String("0-471-34609-8");
    int counthyphen = 0, countdigits = 0;

    //Begin "for" loop.
    for (int i = 0; i < s1.length()-1; i++)
    {
        /////////////////////////////////
        // Check for missing hyphens //
        if (s1.charAt(1) != '-')
        {
            i = s1.length();
        }
        else if (s1.charAt(11) != '-')
        {
            i = s1.length();
        }

        // Now add to the count values //
        if (s1.charAt(i) == '-')
        {
            counthyphen++;
        }
        **else if (s1.substring(i,i+1).matches("[0-9]"))**
        {
            countdigits++;
        }
        /////////////////////////////////
    }

    int i = s1.charAt(s1.length()-1);
    //Check if it's an ISBN and print result.
    **if (counthyphen == 3 && countdigits == 9 && (s1.substring(i, i+1).matches("[0-9]") || s1.substring(i, i+1).matches("X")))**
    {
        System.out.print("This number is an ISBN.");
    }
    else
    {
        System.out.print("This number is NOT an ISBN.");
    }
}
}
  • 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-14T16:55:10+00:00Added an answer on June 14, 2026 at 4:55 pm
    int i = s1.charAt(s1.length()-1);
    

    This code stores the ASCII code of the character at the index : – s1.length() - 1, that can certainly be greater than the maximum accessible string index.

    For e.g, the last character in your current string is 8, whose ASCII code is: – 56, and that would certainly fail.

    So, s1.substring(i, i+1) in your if condition after that would fail.

    In fact, I don’t understand the need of that line at all. Why are you using it?


    Also, your if-else block seems buggy to me: –

        if (s1.charAt(1) != '-')
        {
            i = s1.length();
        }
        else if (s1.charAt(11) != '-')
        {
            i = s1.length();
        }
    

    Why have you assigned same value to i in both the blocks there?

    May be you wanted something like this: –

        if (s1.charAt(1) != '-' || s1.charAt(11) != '-')
        {
            break;  // Just break if not a valid string
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I have been unable to fix a problem with Java Unicode and encoding. The
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.