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 4619732
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:27:51+00:00 2026-05-22T02:27:51+00:00

In below code I am trying to do substring based on start and end

  • 0

In below code I am trying to do substring based on start and end index,But in end of the string.System is throwing ArrayIndexOutOfBoundsException. Please let me know, how to resolve this issue.

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int initlength = 20;
        int start = 0;
        String s = "Some people confuse acceptance with apathy, but there's all "+
"the difference in the world";
        int total=(int)Math.ceil((double)s.length()/(double)initlength);
        for (int i = 0; i < total; i++) {
            System.out.println("s length" + s.substring(start, initlength));
            start = initlength + 1;
            initlength = initlength + initlength;
            }
    }

Regards,

chaitu

  • 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-05-22T02:27:52+00:00Added an answer on May 22, 2026 at 2:27 am

    step by step debug your code:

    1. first time variables

      start=0;
      initlength=0;
      s="Some people confuse acceptance with apathy, but there's all the difference in the world";
      total = 5.

    2. s.length()/initlength = 4.

    3. first substring will be from 0 to 20.
    4. start = 21 and initlenght = 40
    5. Second time in loop
    6. s.length()/initlength = 2
    7. substring from 21 to 40.
    8. start = 41 and initlength = 80.
    9. Third time in loop.
    10. s.length()/initlength = 1 and i equal to 2 therefore loop will break and program execution will finish.

    Based on your edit. Now it will loop for 5 times. And after 3rd time start = 81 and initlength = 160 which is out of range of string. For all the time total = 5.


    If you want that it will get the remaining part the try this:

        int initlength = 20;
        int start = 0;
        String s = "Some people confuse acceptance with apathy, but there's all "
                + "the difference in the world";
        int total = (int) Math.ceil((double) s.length() / (double) initlength);
        for (int i = 0; i < total; i++) {
            if(initlength<s.length()){
                System.out.println("s length" + s.substring(start, initlength));
                start = initlength + 1;
                initlength = initlength + initlength;
            } else {
                initlength = s.length();
                System.out.println("s length" + s.substring(start, initlength));
                break;
            }
        }
    

    output:-

    s lengthSome people confuse 
    s lengthcceptance with apat
    s lengthy, but there's all the difference in th
    s length world
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the code below to send messages via System.Net.Mail and am
So Im trying to make an object dynamically, please see below code from my
I am trying to get the below code to work, but with no luck;
I'm trying to load loadingForm like below code. But it doesn't work, the loadingForm
In the code below I'm trying to load some images and put them in
So far i have got the code below which works lovely when trying an
In my code attached below, I'm trying to upload a file via ASP.NET. I
I am trying to execute the code below to list the select item in
I'm trying to connect to a website (source code below) that requires login and
Im trying to create a function to check for a substring within a string

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.