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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:17:16+00:00 2026-05-16T07:17:16+00:00

I am trying to convert a String into an ArrayList. For example, my Struts2

  • 0

I am trying to convert a String into an ArrayList. For example, my Struts2 webapp returns this String named row in a format similar to this:

[A, BB, CCC, DDDD, 1, 0, 1] (something along those lines)

I need to convert them into an ArrayList so I can prepopulate some forms in another JSP page. I hardcoded a method to convert such Strings into list form:

        StringBuffer rowBuffer = new StringBuffer(row);
        int startIndex = 0;
        int endIndex = rowBuffer.indexOf(",") - 1;
        rowBuffer.deleteCharAt(rowBuffer.indexOf("["));
        rowBuffer.deleteCharAt(rowBuffer.indexOf("]"));
        while(startIndex != -1 && endIndex != -1 && startIndex < endIndex)
        {
          String subString = rowBuffer.substring(startIndex, endIndex);

       if(subString.contains(","))
       {
          rowList.add(" ");
          startIndex = endIndex + 1;
          endIndex = rowBuffer.indexOf(",", startIndex);
       }

       else
       {
        rowList.add(subString);
        startIndex = endIndex + 2;
        endIndex = rowBuffer.indexOf(",", startIndex + 1);
       }

       if(endIndex == -1)
       {
          rowList.add(rowBuffer.substring(startIndex));
          break;
       }
    }

This works fine in cases where all the fields are populated. However, lets say I have a String that looks like this: [A, BB, , , 1, 0, 0] (the 3rd and 4th fields are missing), then I get something that doesn’t work (the blank elements don’t register correctly, and the size of the list is 6, when it should be 7). Is there a more elegant solution than hardcoding? If not, could someone point me in the right direction on how to handle cases with blank fields? Thanks!

  • 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-16T07:17:16+00:00Added an answer on May 16, 2026 at 7:17 am

    Well I changed my code a bit and made it work (it’s probably far from the most elegant solution, but it works for me…

        StringBuffer rowBuffer = new StringBuffer(row);
        int startIndex = 0;
        int endIndex = rowBuffer.indexOf(",") - 1;
        rowBuffer.deleteCharAt(rowBuffer.indexOf("["));
        rowBuffer.deleteCharAt(rowBuffer.indexOf("]"));
        while(startIndex != -1 && endIndex != -1 && startIndex < endIndex)
        {
          String subString = rowBuffer.substring(startIndex, endIndex);
    
           if(subString.contains(","))
           {
              rowList.add(" ");
              startIndex = endIndex - 1;
              endIndex = rowBuffer.indexOf(", ", startIndex + 1);
           }
    
           else
           {
             if(subString.equals("1"))
                 rowList.add("True");
             else if(subString.equals("0"))
                 rowList.add("False");
             else
              rowList.add(subString);
            startIndex = endIndex + 2;
            endIndex = rowBuffer.indexOf(",", startIndex + 1);
           }
    
           if(endIndex == -1)
           {
              if(subString.equals("1"))
                 rowList.add("True");
              else if(subString.equals("0"))
                 rowList.add("False");
              break;
           }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 530k
  • Answers 530k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I realized I was using a very very large version… May 16, 2026 at 11:41 pm
  • Editorial Team
    Editorial Team added an answer My personal preference is simplexml_load_string since it makes handeling XML… May 16, 2026 at 11:41 pm
  • Editorial Team
    Editorial Team added an answer A tangential answer: Perhaps you need to try an alternative… May 16, 2026 at 11:41 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm trying to convert a string into money format using this function , and
I am trying to convert a string into date time format. DateTime.Parse(tempfrmBankDetails.dgvBankDetails.SelectedRows[0].Cells[PaymentDate].Value.ToString(),null); This is
I am trying to convert a string into datetime with the following C# code,
I'm trying to convert my string into a dynamic array of doubles. Each space
I'm trying to convert a string 20091229050936 into 05:09 29 December 2009 (UTC) >>>import
I am trying to convert a standard std::string into an NSString but I'm not
I'm trying to convert this function into PHP but somehow it does not give
I having a problem converting a string into the date format I need. An
I have a string in an Android app that I am trying to convert
i m trying to make a program to convert a number into it's binary.

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.