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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:55:27+00:00 2026-06-13T14:55:27+00:00

In searching a pre-populated ArrayList, call it List<String> myList= new ArrayList<String>(); I am trying

  • 0

In searching a pre-populated ArrayList, call it

List<String> myList= new ArrayList<String>();

I am trying to add new elements to it based on these elements containing certain regex values. I am having success when I just need to return one substring of the element. For example, if I have

String removable = "X";

which resides in an element, call it

ABCDX

the X is easily removed and added to the ArrayList with the code

   if(myList.get(i).length() == 5 && myList.get(i).substring(4,5).contains(removable))
   {
            myList.add(i+1, myList.get(i).substring(0, 4));
   }

but when I have an element such as

ABXCD ,

which I presumed could be handled with

 if(myList.get(i).length() == 5 && myList.get(i).substring(2,3).contains(removable))
   {
            myList.add(i+1, myList.get(i).substring(0, 2).substring(3, 5));
   }

I get a String Index Out of Bounds Exception. How can this be rectified?

  • 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-13T14:55:28+00:00Added an answer on June 13, 2026 at 2:55 pm

    In your second code example you are using

    abc1List.get(i)
    

    should this not be

    myList.get(i)
    

    Edit

    I believe your problem is that you are substring’ing a substring. Your code does this

    myList.get(i).substring(0, 2).substring(3, 5)
    

    So what that is saying is, get the substring of a string, then get a substring of the previously retrieved substring. Since the first substring is only 2 characters long, the second substring returns out of bounds as you are trying to start from index 3 which does not exist.

    Your code pretty much evaluates to:

    "ABXCD ,".substring(0,2) = "AB"
    
    "AB".substring(3,5) = Out of bounds substring retrieval!
    

    You need to do something like

    myList.get(i).substring(0, 2) + myList.get(i).substring(3, 5)
    

    or find an alternative method of doing what you want.

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

Sidebar

Related Questions

searching around trying to find a way to add index on suffix of a
Searching a Python dictionary based on the value first, to get a key output
I'm trying to find certain keywords in a string with python. The string is
I'm searching a solution to have a pre-validation for a nested resource before the
I am rather new to ZendFramework and am trying to figure this out. In
I'm searching on Google but most of the results I'm getting have these complicated
I'm trying to set seletected value to a pre-loaded combobox using silverlight with MVVM.
I'm searching through a list of images and I want to find the best
I'm debugging a certain method in a pre-existing project (call it foobarPressed). It's listed
I'm not exactly sure what to call the thing i am searching for. If

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.