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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:28:46+00:00 2026-05-31T12:28:46+00:00

Why does the split method not return an array with 2 elements? for(int i

  • 0

Why does the split method not return an array with 2 elements?

for(int i = 0; i < temparray.size(); i++)
{
if (temparray.get(i).contains("_"))
    System.out.println("True" + temparray.get(i).length() + " " + temparray.get(i));
String[] temp = temparray.get(i).split("_");
System.out.println(temp[0]);
//System.out.println(temp[1]);
//friendsOld.add(new Friend(temp[0],temp[1]));
}

If I uncomment either of the lines, I get ArrayOutofBoundsException: 1. The println always returns True, the length of the String, and then a String with _ located within the String – NOT at the end.

I’ve tried negative parameters for .split(), converting the String to char arrays and breaking the String using indexOf() to find the location of _ then splitting it manually using substring(). There might be something wrong with the String itself but here is the code for the array of Strings: ArrayList<String> temparray = new ArrayList<String>();.

  • 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-31T12:28:47+00:00Added an answer on May 31, 2026 at 12:28 pm

    It seems that you forgot the braces after the if-statement:

    if (temparray.get(i).contains("_")) {
        System.out.println("True" + temparray.get(i).length() + " " + temparray.get(i));
        String[] temp = temparray.get(i).split("_");
        System.out.println(temp[0]);
        System.out.println(temp[1]);
        friendsOld.add(new Friend(temp[0],temp[1]));
    }
    

    The way you wrote it, the string is splitted even when it doesn’t contain an underscore. Only the output of “True […]” is limited to strings containing one.

    You should start using the debugger – it will display the values of variables when hitting an exception breakpoint allowing you to further track down the bugs in your code.

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

Sidebar

Related Questions

dir(re.compile(pattern)) does not return pattern as one of the lists's elements. Namely it returns:
I can split editor panes horizontally or vertically, but it does not seem possible
I cannot understand why this method is not working. Does it look at the
HtmlHelper.GetTagsAndValues(htmlContent); and i get this error: at System.String.Split(String[] separator, Int32 count, StringSplitOptions options) at
My client has a multi-page PDF file. They need it split by page. Does
Does anyone know how to get IntelliSense to work reliably when working in C/C++
Why does the following not work in Java? It would work in C#: public
The Django jQuery fix which is given here does not work on iPad. Any
When I try to split a String around occurrences of . the method split
In my code I am using requestRouteToHost() method: Does this routing means changing the

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.